I have an ASP.NET MVC 4 project and I’ve added a Web API controller to it. Nothing fancy, no custom configuration, nothing. It doesn’t work out of the box. I get a 404 HTTP error and this error stack in my IIS logs:
[HttpException]: The controller for path '/api/controllername/5' was not found or does not implement IController. at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Turns out to be a pretty non-obvious fix. The error mentions that it’s checking that the controller implements IController. Normal MVC controllers implement IController, but WebAPI controllers do not. So it’s trying to match our URL with a standard MVC controller.
Take a look in the RouteConfig file and you’ll see something like this:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Does that match your URL? Yeah, it probably does. Your URL (if you’re following the normal WebApi pattern) starts with ‘/api/’, so the router is trying to find a standard controller called ‘api’
Open up the file Global.asax.cs. It’ll look something like this:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); GlobalConfiguration.Configure(WebApiConfig.Register); }
You will now notice that the RouteConfig (for normal controllers) is registered in the pipeline before the WebApiConfig routes. This means that the standard controllers take precedent. And because your Web Api URL matches the pattern defined in RouteConfig, the framework is attempting to use it.
The fix is just to move the WebApiConfig registration further up the pipeline, before RouteConfig:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); GlobalConfiguration.Configure(WebApiConfig.Register); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
THANK YOU!! It was really really helpful!
This is useful but in my case my WebApiConfig does not add any routes at all. The Register() function is a noop. Yet this exception is still thrown. For me it triggeres IIS to follow Error 500 handling instead of a 404.
Thanks……..really thanks!!! you saved my life
Thanks….
Thanks ,really helpful.
Thank you. This has been driving me crazy for days.
Thanks…worked like a charm
Thank you. This worked for me. ))
Thank you Jonathan, this was a huge help!!
Thank You sooooo Veryyy much. Worked for me.
I have checked your site and i’ve found some duplicate
content, that’s why you don’t rank high in google’s search results, but there is a tool that can help you
to create 100% unique articles, search for; Boorfe’s tips unlimited content
Thank you very much. nice catch.
It worked for me
Chased tail around for a while until this was found. Thank you!
ZtD0ch Your stream posts constantly contain a lot of especially positive to date information. Everyplace achieve you come positive with this? Emphatically stating you are same creative. Recognition again
Thank you very much!!!!!!!! Life Saver!!!!