Daily Archives: 3 September 2014

WebAPI Method Returning 404

There is something I absolutely hate when programming. When you hit a bump in the road that brings everything to a halt; and it is something which should definitely not be getting in the way. I hesitate to call them bugs. It’s just those little annoyances where I have overlooked some little thing and it has cost me hours.

Welcome to my day today. I had an ASP.NET MVC application which, when I created it, did not have the WebAPI stuff in the solution (I deliberately unchecked the checkbox which would have included all that stuff). So, when I decided that I did, in fact, want to use the WebAPI, I manually added all the necessary nuget packages etc.

There was one thing I got wrong. And troubleshooting this took me around 1.5 hours. Apparently, you need to register the WebAPI routes before the standard MVC routes:

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

I’m not saying it shouldn’t be that way. It’s just that, when you are trying to get that thing, which should have taken 5 minutes, working 90 minutes later, you get a little peeved. Especially when you hear the clock ticking and time slipping away. In effect, it’s not even having wheels on the ground until the annoying oversight is rectified.

Filing this one under stuff I do not want to forget. Ever.