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:
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With