3

我刚刚在我的 WebAPI 项目上更新(从 v3.x)到最新版本的 AttributeRouting,它刚刚开始产生我以前从未见过的错误。

现在,每当调用 API 时,我都会收到如下错误:

System.InvalidOperationException: The constraint entry 'inboundHttpMethod' on the route with route template 'my/path' must have a string value or be of a type which implements 'IHttpRouteConstraint'.
   at System.Web.Http.Routing.HttpRoute.ProcessConstraint(HttpRequestMessage request, Object constraint, String parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection)
   at System.Web.Http.Routing.HttpRoute.ProcessConstraints(HttpRequestMessage request, HttpRouteValueDictionary values, HttpRouteDirection routeDirection)
   at System.Web.Http.Routing.HttpRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)
   at AttributeRouting.Web.Http.Framework.HttpAttributeRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)
   at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)
   at System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext)
   at System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context)
   at System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

它已经工作了几个月没有问题。

没有文档详细说明有任何使用更改。我的配置文件看起来正确。

出了什么问题?我找不到其他人报告这个。

4

1 回答 1

5

绝对是内存托管类型问题。最近,我在更新到 MVC 5 后遇到了同样的问题。我查看了各种帖子,实际上找到了一个最终帮助我的帖子。这是我解决这个问题的方法。

为属性路由安装库后,您必须在 global.ascx 文件中有以下内容。

AttributeRoutingHttpConfig.RegisterRoutes(GlobalConfiguration.Configuration.Routes);

在 AttributeRoutingHttpConfig 类中,替换以下代码:

routes.MapHttpAttributeRoutes();

routes.MapHttpAttributeRoutes(cfg =>
{
            cfg.InMemory = true;
            cfg.AutoGenerateRouteNames = true;
            cfg.AddRoutesFromAssemblyOf<ANY_API_Controller>();
        });

在这里, ANY_API_Controller 指的是您项目中的任何 apicontroller 类。

于 2013-11-13T17:08:31.717 回答