9

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?

17
  • Have you tried to set [HttpGet] attribute for Get() action?
    – Roman
    Feb 1, 2017 at 9:33
  • @Roma Web API v2 automatically maps the HTTP verb based on the name of the method - this should not be necessary
    – ADyson
    Feb 1, 2017 at 9:34
  • Can you double-check that your WebApiConfig.Register method is actually hit?
    – CoolBots
    Feb 1, 2017 at 9:36
  • @Roma just tried [HttpGet], same result.
    – youen
    Feb 1, 2017 at 9:39
  • @Anestis Kivranoglou Just tried this URL, doesn't work either (same error).
    – youen
    Feb 1, 2017 at 9:39

1 Answer 1

6

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

1
  • 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.
    – youen
    Jul 3, 2017 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.