Reputation: 1279
I may have a lame question, but I am struggling a bit with routing of multiple controllers in one Url. So my question is how to work with a url like this:
GET http://foo.com/API/Devices/2/Parameters/2
where eventually device
and parameters
are all controllers and numbers are Ids. I have controllers for each of them but how do process them, route them to proper controller?
Any direction to look at?
UPDATE:
Just for clarification the solution which I ended up using and follows the answer below.
Routing snippet:
config.Routes.MapHttpRoute(
name: "DeviceCommandsControllerHttpRoute",
routeTemplate: "api/devices/{deviceId}/commands/{id}",
defaults: new { controller = "devicecommands", id = RouteParameter.Optional }
);
Controller snippet:
[HttpGet]
public DeviceCommand GetById(int id, int deviceId)
{ ... }
and finally the URL:
GET http://localhost:49479/api/Devices/2/Commands/1
Upvotes: 5
Views: 7341
Reputation: 58444
I am working with a smilar need right now and I have the following route structure:
public static void RegisterRoutes(HttpRouteCollection routes) {
routes.MapHttpRoute(
"UserRolesHttpRoute",
"api/users/{key}/roles",
new { controller = "UserRoles" });
routes.MapHttpRoute(
"AffiliateShipmentsHttpRoute",
"api/affiliates/{key}/shipments",
new { controller = "AffiliateShipments" });
routes.MapHttpRoute(
"ShipmentStatesHttpRoute",
"api/shipments/{key}/shipmentstates",
new { controller = "ShipmentStates" });
routes.MapHttpRoute(
"AffiliateShipmentShipmentStatesHttpRoute",
"api/affiliates/{key}/shipments/{shipmentKey}/shipmentstates",
new { controller = "AffiliateShipmentShipmentStates" });
routes.MapHttpRoute(
"DefaultHttpRoute",
"api/{controller}/{key}",
new { key = RouteParameter.Optional });
}
I think you can sort of figure out how you can implement yours based on this.
Upvotes: 9