c# - How can I structure a REST API endpoints for MS Web Api cleanly? -


i have data model called pim (product info manager) requires pass company appid every request. if want products pass company appid , resource id.

i'm trying structure rest endpoints in clean manner , i've come this:

http://api.example.com/pim/44/products/    -- gets products in company 44 http://api.example.com/pim/44/products/123 -- product 123 in company 123 

is best way implement this?

i planning making default controller pim/{appid}/{controller}/{id}

will run problems mvc routes if this? how access {appid} token in method?

you can have route described, , use

routes.maphttproute(     name: "default",     routetemplate: "pim/{appid}/{controller}/{id}",     defaults: new { id = routeparameter.optional } ); 

and in controller you'd have following methods:

public class productscontroller : apicontroller {     public product get(int appid, int id) { ... }     public list<product> get(int appid) { ... } } 

since parameter names (appid, id) match route parameters, asp.net web api should bind url value parameter when operation called.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -