我刚刚在我的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)
几个月来它一直没有问题。
非文档详细信息有任何用法更改。我的配置文件看起来是正确的。
出了什么问题?我找不到其他人举报此事。
答案 0 :(得分: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类。