1

Let's say I have an app with orders and order details. I am trying to figure out how to best set up the architecture so I can retrieve all order details for a particular order. It would seem elegant and intuitive to have a URL structure like this:

http://example.com/api/orders/{orderid}/orderdetails

I can register a route in global.asax:

System.Web.Routing.RouteTable.Routes.MapHttpRoute(
  name: "DefaultApi",
  routeTemplate: "api/orders/{orderid}/orderdetails",
  defaults: new { orderid = System.Web.Http.RouteParameter.Optional }
);

But I can't figure out what method in the OrdersController class this maps to. What method signature does the above route expect in my controller class? For all I know, this isn't even valid, so feel free to recommend a different approach.

4 Answers 4

3

I got it working with the following.

Global.asax Application_Start:

System.Web.Routing.RouteTable.Routes.MapHttpRoute(
 name: "DefaultApi",
 routeTemplate: "api/{controller}/{orderid}/{action}"
);

Controller class:

[ActionName("OrderDetails")]
public IEnumerable<OrderDetail> GetOrderDetails(int orderId)
{
 // return data...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at amazing AttributeRouting.WebAPI package http://www.strathweb.com/2012/05/attribute-based-routing-in-asp-net-web-api/.

Comments

0

Take a look at the following page. It will be helpful http://aspnetwebstack.codeplex.com/wikipage?title=Attribute%20routing%20in%20Web%20API

Comments

0

Add System.Web.Http.WebHost dll reference into your web application. Thank You.

1 Comment

This doesn't answer the OP question. He asked which method and signature in the OrdersController class his route maps to.

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.