ASP.Net WebApi Error: “The controller for path … was not found or does not implement IController.”

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);
}

11 thoughts on “ASP.Net WebApi Error: “The controller for path … was not found or does not implement IController.”

  1. 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.

  2. 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

Leave a Reply to Sam Cancel reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>