My Web API route won’t match

Its probably because your routes are in the wrong order.

Back to blog

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,

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.