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?
[HttpGet]
attribute forGet()
action?