Friday, May 16, 2014

HTTP Error 404 on WebAPI 2 Routes - Fix

ASP.NET Web API 2 Rocks.

I was using built in Attribute Routing and everything else worked fine But, I faced a crazy error 404 on my routes when I was declaring this route.

[RoutePrefix("api/lookups")]
public class LookupsApiController : ApiControllerBase

{
        [Route("")]
        [HttpGet]
        public HttpResponseMessage Get()
        {
            return Request.Success(1);
        }
}

I was going crazy over this error. I Installed "WebAPI 2 Help Page" [Install-Package Microsoft.AspNet.WebApi.HelpPage] and Tracing [Install-Package Microsoft.AspNet.WebApi.Tracing], the routes are registered and shows in the help page.

But it wasn't working.

And then found this answer in SO. (http://stackoverflow.com/a/21207704/959245)
It says that I was using conventional routes. "note that requests matching conventional routes can never reach attributed controller/actions."

My fix?
change the route prefix like this.

[RoutePrefix("api/data/lookups")]

and access http://localhost:xxx/api/data/lookups/countries

It works!