5

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 });
2
  • 3
    Did you call config.MapHttpAttributeRoutes();? Commented Jan 21, 2015 at 12:50
  • @haim770 yes, that is the solution. If you create an answer, i will accept it, thankl you! Commented Jan 21, 2015 at 13:00

7 Answers 7

7

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

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not following how this class is actually called?
@leen3o Usually, in your Global.asax
How would you call this in the global.asax? I don't get where HttpConfiguration comes from.
7

Check your Route attribute's namespace. It should be System.Web.Http instead of System.Web.Mvc.

1 Comment

Can't upvote more than once :) This is usually the solution to this problem, when you create controlles via rightclick->new controller.
6
try adding a forward slash after the tilde
[HttpGet]
[Route("~/api/admin/template/{fileName}")]
public HttpResponseMessage Template(string fileName)
{
    return CreateHtmlResponse(fileName);
}

Comments

3

Verify that you are using System.Web.Http.RouteAttribute and not System.Web.Mvc.RouteAttribute

Comments

1

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.

4 Comments

This routing is for standard MVC controllers, not for API controllers.
@haim770, That's actually incorrect... MapHttpRoute is for Web Api. MapRoute is for MVC.
@Slick86, See the initial revision
Aah, Sorry, missed that
1

With my Web API 2 project I had to add a [RoutePrefix("events")] to the controller for it to pickup the action route attribute.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.