asp.net mvc - REST API nested resources should be handled by which controller? -


let's have rest api endpoint: users/123/orders/234.

as uri implies, returning order 234 user 123. question is, controller should handle request?

should action in userscontroller, getorder(int userid, int orderid)? or should handled in orderscontroller getorder(int id)?

is matter of taste, or 1 way more "correct" other?

well advocate should go entity being fetched.

in case what being fetched? : orders -> orderscontroller.

if users being fetched given particular order id userscontroller.

you should have @ stackexchange api examples : http://api.stackexchange.com/docs

there numerous actions, they're each grouped entity operate on, , bet that's controller in.


there no inbuilt setup route. following :

this specific route.

routes.maphttproute(     name: "users_orders",     routetemplate: "api/{controller}/{user_id}/orders/{order_id}",     defaults: new     {         controller = "orders",         action = "fetchbyuser"     }); 

which need action method this:

public actionresult fetchbyuser(int user_id, int order_id) { } 

you try doing more generalised route this:

routes.maphttproute(     name: "fetch_route",     routetemplate: "api/{controller}/{id1}/{type}/{id2}",     defaults: new     {         action = "fetch"     }); 

and action method be:

public actionresult fetch(int user_id, string type, int order_id) { } 

note: interpret -> users/123/orders/234 user 123 order 234. if @karan says don't need user context should not have method. i'm not sure design or requirements here.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -