Imagine a system with Users, Groups with corresponding ApiControllers
.
Then imagine 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 responsibility of #2 to #1's Get(int? groupId)
method?
I'd like to keep responsibility of handling sub-resources with their original Controller.
In another words, If a sub-resource also exists as resource then sub-resource handling should be? delegated to primary resource controller...
P.S. Now, I am not sure if the above approach is "cosher" with RESTfull styles, that is whole other discussion...
The four main HTTP methods (GET, PUT, POST, and DELETE) can be mapped to CRUD operations as follows: GET retrieves the representation of the resource at a specified URI.
The main difference is: Web API is a service for any client, any devices, and MVC Controller only serve its client. The same because it is MVC platform.
Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder. The name of a controller class must end with "Controller" and it must be derived from System. Web.
WEB Api Beta isn't supporting method level attribute for routing. They said, that are thinking of improving the routing story for the next release.
For now the only way (as i know) is to map explicitly those routes. In your case:
// for your rest style api
routes.MapHttpRoute(
name: "UserGroups",
routeTemplate: "api/groups/{groupID}/users",
defaults: new { controller = "Users"},
constraints: new { groupID = @"\d+" }
);
and for RPC URI style the default route will work as glance.
routes.MapHttpRoute(
name: "ApiDefault",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Users", id = RouteParameter.Optional }
);
And also you can override DefaultHttpControllerFactory class to create in your way instances of controllers.
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With