I'm trying to get this to work in my ASP.Net Web API 2 application. You will notice that this Controller inherits Controller. This is because I need to return a View instead of JSON.
[RoutePrefix("api/Manage")]
public class ManageController : Controller
{
[Route("TestOne")]
public async Task<ActionResult> MyTestOne(string value1, string value2)
{
return View("");
{
}
Here is the error I'm getting.
<error>
<MessageDetail> No type was found that matches the controller named 'Manage'.</MessageDetail>
</Error>
I need to call the Manage Controller like so.
https://api.domain.com/api/Manage/TestOne?value1=foo&value2=bar
My RouteConfig is configured like so.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
NOTE: [RoutePrefix("api/Account")] works in my AccountController. This is an API Controller and inherits ApiBase.
Any help is much appreciated! Thanks!
It happens because you have 2 route configuration, one for MVC
controllers and one for Web API
. And in your case Web API
route configuration goes first. Global.asax.cs
looks like this
//some configs
WebApiConfig.Register(GlobalConfiguration.Configuration);
//some configs
RouteConfig.RegisterRoutes(RouteTable.Routes);
And you must be having something like this in Web API
route config
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
When you request /api/Manage/TestOne
the Web API
routing applies first. No attribute based route fits but the request perfectly matches to DefaultApi
route. Manage
matches to {controller}
and TestOne
goes to {id}
. So the framework starts searching for api controller with name Manage
like this
public class ManageController : ApiController
But there is not such controller and indeed you have an error
{
"Message": "No HTTP resource was found that matches the request URI 'http://host/api/Manage/TestOne/?value1=foo&value2=bar'.",
"MessageDetail": "No type was found that matches the controller named 'Manage'."
}
So I can suggest you few possible solutions.
Change route configuration order
//some configs
RouteConfig.RegisterRoutes(RouteTable.Routes);
//some configs
WebApiConfig.Register(GlobalConfiguration.Configuration);
And then your example will work as expected but it may create unexpected errors because I don't know all possibles routes in your application.
Remove DefaultApi
route
If you completely rely on attribute based routing for Web API
you can just remove this configuration with no negative effect for you application
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Or just change prefix
If you change prefix from api
to anything else it will work as well because it won't match DefaultApi
route anymore
[RoutePrefix("view/Manage")]