Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net web api 2 Route-Attribute not working

I've the following problem, my route attribute is not working.

I have the following action:

[HttpGet]
[Route("~api/admin/template/{fileName}")]
public HttpResponseMessage Template(string fileName)
{
    return CreateHtmlResponse(fileName);
}

and i want to access the action like .../api/admin/template/login.html, so that Template get login.html passed as the file name.

But i alsways get: No HTTP resource was found that matches the request URI 'http://localhost:50121/api/admin/template/login.html'.

The following request works: /api/admin/template?fileName=login.html

Does anyone know, what i am doing wrong with my routing?

EDIT:

My route configuration

config.Routes.MapHttpRoute(
                    "API Default", "api/{controller}/{action}",
                    new { id = RouteParameter.Optional });
like image 384
BendEg Avatar asked Nov 28 '22 07:11

BendEg


1 Answers

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

like image 74
haim770 Avatar answered Dec 13 '22 11:12

haim770