Web API 2 - Route Not Found (404)

It worked before, now it doesn't?

Here's the checklist.


  • Web API routes registration.

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

Web API config should come before normal Routes.

  • Route Prefix
 change [Route("~/api/sendemail")] to [Route("/api/sendemail")]

Prefix "~" is not needed.

  • Route values
[Route("{myId:int}/sendemail")]
[HttpPost()]
public bool SendEmail(int id, ContactModel data)
{
    return Ok();
}
notice the route defines a myId but method accepts an id. This will give a 404
Change the id or myId like this for it to work.

[Route("{myId:int}/sendemail")]
[HttpPost()]
public bool SendEmail(int myId, ContactModel data){
    return Ok();
}

Popular posts from this blog

Print a receipt using a Thermal Printer with C#.NET

Automatic redirect upon session timeout using ASP.NET MVC and Javascript

Complex Master-Detail Form using Knockout.js and ASP.NET MVC