3

I cannot figure out for the life of me why my attribute routing isn't working.

Here is my setup:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing
        config.MapHttpAttributeRoutes();

        // Convention-based routing
        config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

    }
}

Here is my controller with my routing attributes:

[Route("api/v1.0/orders")]
public class OrdersV1Controller
{

    [APIAuthentication(RequireAuthentication = true)]
    [HttpGet]
    [Route("{id:int}")]
    public GetOrderResponse GetOrder(int id)
    { 
      .....
    }
}

Here is my global asax file:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Populate;
    }
}

Here is the URL i'm testing which is returning a 404 not found:

http://localhost:60105/api/v1.0/orders/111111

3
  • 1
    have you considered public class OrdersV1Controller : ApiController Commented May 13, 2014 at 13:20
  • Wow i can't believe i missed that in all the examples... argh... that did it man.. add it as an answer so i can give you credit Commented May 13, 2014 at 13:21
  • hey - you and me both, i've been working on this for 2 days... lmao.. i feel so stupid.. haha thanks again man Commented May 13, 2014 at 13:22

1 Answer 1

7

your controller needs to be an API Controller :

public class OrdersV1Controller : ApiController
Sign up to request clarification or add additional context in comments.

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.