Can ASP.NET Web API handle subresource with different controllers

Imagine a system with users, groups with the appropriate ApiControllers. Then imagine the following access patterns:

/api/users                  
/api/users/1
/api/users?groupId=1    <- #1 returns users belonging to group id 1 via Get(int? groupId)

/api/groups/
/api/groups/1
/api/groups/1/users     <- #2 sub resource, returns users belonging to group id 1

Is it possible to delegate the responsibility of method # 2 to # 1 Get(int? groupId)? I would like to be responsible for processing the sub-resources using my original controller. In other words, if a sub-resource also exists as a resource, should processing of the sub-resources be performed? delegated to the main resource controller ...

PS Now I'm not sure if the aforementioned "kosher" approach with RESTfull styles, that is, all the other discussion ...

+5
source share
1 answer

WEB Api Beta . , .

( ) - . :

 // for your rest style api 
 routes.MapHttpRoute(
     name: "UserGroups",
     routeTemplate: "api/groups/{groupID}/users",
     defaults: new { controller = "Users"},
     constraints: new { groupID = @"\d+" } 
 );

URI RPC .

 routes.MapHttpRoute(
           name: "ApiDefault",
           routeTemplate: "api/{controller}/{id}",
           defaults: new { controller = "Users", id = RouteParameter.Optional }
       );

DefaultHttpControllerFactory, .

.

+6

All Articles