March 14 2014

ASP.NET Web API - elmah.axd 404 error - No HTTP resource was found that matches the request URI 'http://.../elmah.axd'

If you've installed ELMAH on your ASP.NET Web API project and get the error "No HTTP resource was found that matches the request URI 'http://.../elmah.axd'" then you may have done the same thing as me.

When I first got this error I thought maybe ELMAH just didn't play nicely with Web API yet, but just to double check I installed it on a fresh new Web API project and it worked fine! I then went through a long and agonising process of elimination to figure out what in my application was preventing ELMAH from displaying before finally I found the culprit.

The problem was that I had removed the 'api' prefix from the default route in WebApiConfig.cs, so instead of "api/{controller}/{id}" I have just "{controller}/{id}". This is because my api is running on it's own subdomain "api.mydomain.com" rather than having it sitting off the primary domain as "mydomain.com/api".

To fix the issue I added the following ignore rule to Global.asax.cs:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

The thing that had me stumped for a while was that ignoring the route in the WebApiConfig.cs didn't work, you have to ignore it on the RouteTable.Routes (RouteCollection) collection, not on the config.Routes (HttpRouteCollection) collection.


Sponsored by