Asp.net WebApi: no types were found that matched the controller named "xxxx"

I have this problem, which seems pretty common, but none of the solutions I read worked in my case. I am trying to add ApiController to 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";
        }
    }
}

. , , . - ?

+4
1

, , , .

. :

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

DefaultHttpControllerTypeResolver WebHostHttpControllerTypeResolver. WebHostHttpControllerTypeResolver (), , . , WebHostHttpControllerTypeResolver DefaultHttpControllerTypeResolver.

, -api, API. , . , -API.

, . MS-ApiControllerTypeCache.xml - C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root.

WebHostHttpControllerTypeResolver.cs

+1

Source: https://habr.com/ru/post/1668496/


All Articles