asp.net webapi 2 attribute routing not working
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.
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);
}
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();
.