Asp.net WebApi : No type was found that matches the controller named 'xxxx'

11,204

I know this is an old issue, but since it never got an accepted answer, I'll give it a go.

I had this exact issue as well. For me, this solved the issue:

GlobalConfiguration.Configure(config =>
{
    config.Services.Replace(typeof(IHttpControllerTypeResolver), new DefaultHttpControllerTypeResolver());
    ...
});

In my case, WebHostHttpControllerTypeResolver was used instead of DefaultHttpControllerTypeResolver. WebHostHttpControllerTypeResolver first reads controller types from a cache (a file), and if that file exists, it's content is serialized and returned. If the file don't exist, WebHostHttpControllerTypeResolver calls DefaultHttpControllerTypeResolver instead.

I got the problem by first setting up the web api, but not having any API controllers. That resulted in a cached empty list of controller types, hence the issue. Since the controller types is cached within a file, its contents won't be deleted even though you restart your Web API.

Another solution could be to remove the file that stores the cached list of controller types. The file is called MS-ApiControllerTypeCache.xml and should be somewhere in the folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root.

WebHostHttpControllerTypeResolver.cs

Share:
11,204
youen
Author by

youen

Updated on August 02, 2022

Comments

  • youen
    youen over 1 year

    I have this issue that seems quite common, but none of the solutions I've read about worked in my case. I'm trying to add an ApiController in an existing MVC project.

    When I try to access the added controller on http://localhost:51362/api/test, I get this error: {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:51362/api/test'.","MessageDetail":"No type was found that matches the controller named 'test'."}

    My project contains the following NuGet packages:

    • Microsoft.AspNet.Mvc 5.2.3
    • Microsoft.AspNet.WebApi 5.2.3
    • Microsoft.AspNet.WebApi.Client 5.2.3
    • Microsoft.AspNet.WebApi.Core 5.2.3
    • Microsoft.AspNet.WebApi.WebHost 5.2.3

    My Global.asax.cs (excerpt):

    RouteTable.Routes.Clear();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteTable.Routes.MapMvcAttributeRoutes();
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    

    My WebApiConfig.cs:

    using System.Web.Http;
    
    namespace End.Web
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
                GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }
    }
    

    My new controller:

    using System.Web.Http;
    
    namespace End.Web.Controllers
    {
        public class TestController : ApiController
        {
            public string Get()
            {
                return "ok";
            }
        }
    }
    

    I also tried attribute routing with no success. What I want to do seems really simple. Am I missing something obvious?

  • youen
    youen almost 7 years
    Well, I honestly have no idea if this was my issue too... I don't even remember for which project I had this issue in the first place :) But I'll accept your answer since this looks like it could very well be the same problem.