Its probably because your routes are in the wrong order.
A question I get asked a lot – why wont my web api route match ?
The answer is that in 90% of cases its one of two causes,
1. You are setting up your routes in a non-REST style – Web API uses the HTTP verb to match requests to action methods based on naming convention – read this for a explanation – Routing in ASP.NET Web API
2. You are declaring your default MVC route before your Web API route(s)
For example, if you declare the following routes,
1 2 3 4 5 6 7 8 9 10 11 |
// Default MVC route routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" }); // Default Web API route routes.MapHttpRoute( name: "DefaultRest", routeTemplate: "api/v1/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); |
and submit a GET request to /api/v1/customers, MVC will match the ‘Default’ route as
{controller} = api
{action} = v1
{id} = customers
And throw a 404.
If you declare the ‘DefaultRest’ route first, the same GET request will match to,
{controller}=customers (assuming ‘customers’ inherits from ApiController)
{action} = Get which is normally the required behavior.
Getting your routes in the wrong order is the biggest cause of routing problems in MVC.