I\'ve the following problem, my route attribute is not working.
I have the following action:
[HttpGet]
[Route(\"~api/admin/template/{fileName}\")]
pu
In my case, I added Route("api/dashboard")
to api controller.
Changed it to RoutePrefix("api/dashboard")
. And it works perfectly.
Also you need config.MapHttpAttributeRoutes();
in webapiconfig.cs
With my Web API 2 project I had to add a [RoutePrefix("events")]
to the controller for it to pickup the action route attribute.
You have to call MapHttpAttributeRoutes()
so that the Framework will be able to walk through your attributes and register the appropriate routes upon application start:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
// you can add manual routes as well
//config.Routes.MapHttpRoute(...
}
}
See MSDN
try adding a forward slash after the tilde
[HttpGet]
[Route("~/api/admin/template/{fileName}")]
public HttpResponseMessage Template(string fileName)
{
return CreateHtmlResponse(fileName);
}
Check your Route attribute's namespace. It should be System.Web.Http instead of System.Web.Mvc.
Try this routing in your WebApiConfig
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
You have to add RoutePrefix
.