asp.net webapi 2 attribute routing not working

守給你的承諾、 提交于 2019-11-29 20:55:58

Based on your information, it looks like you are not calling the httpConfig.MapHttpAttributeRoutes() (Make sure to call this before any traditional routing registrations)

Since you haven't called MapHttpAttributeRoutes, your request seems to be matching a traditional route, for example, like api/{controller}. This will not work because routes matching traditional routes will never see controllers/actions decorated with attribute routes.

Josh C

A problem I ran into was related to the ordering in Application_Start(). Note the order of Web API configuraton below:

This does NOT work

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

This does work

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Kevin.Debeil

I had this problem too and after a long search I realized that I was using System.Web.Mvc.RouteAttribute instead of System.Web.Http.RouteAttribute After correcting this and using config.MapHttpAttributeRoutes() everything worked fine.

This was not your case (as is apparent from your sample code), but please do remember to end the Controller class name with Controller.

Else it won't be picked up by config.MapHttpAttributeRoutes();.

This question already has a selected answer. But I had a different solution for myself and think it would be helpful to reply if the selected answer doesn't help.

For me it was a silly mistake. I had two controllers but only one was working. The solutions was that my controller class was named improperly!

My working controller-

public class FooController : ApiController { }

My non-working controller-

public class BarControllers : ApiController { }

Be sure your controller class ends in Controller. The trailing s got me!

In my case, VS create my controller with the name

TestController1

I dont know why he put this number "one" in the end of name, but remove and will work.

Make sure you don't have two controllers with the same name! I was moving some controllers from one assembly I was throwing away into the website... whilst the website no longer had references to the old assembly other assemblies did which meant it was copied in to the WebSite bin folder. The route discovery process then seemed to fail silently when it came across two occurrences of the same controller!

In my case following line was creating problem, just commented it and everything start working

config.MapHttpAttributeRoutes();

Comment it in WebApiConfig.cs file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!