asp.net - Web API Nested Resource in URL -
i creating first asp.net web api. trying follow standard rest urls. api return search result records. url should –
../api/categories/{categoryid}/subcategories/{subcategoryid}/records?searchcriteria
i planning use odata searching , basic / digest authentication on iis. problem in nested resources. before return search results, need check whether user has access category , sub category. created visual studio 2012 – mvc4 / web api project start with. in app_start folder, there 2 files believe url , order of resource related.
1.routeconfig.cs
routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional } );
2.webapiconfig.cs
config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } );
with model, works fine if url ../api/records?searchcriteria not url design mentioned above. understand have little more reading far not able find correct article. need advice on how achieve url , changes needed in these 2 files. alternatively, there other configuration missing here? in advance.
asp.net web api 2 provides attribute routing out of box. can define route
on individual action method or @ global level.
e.g:
[route("customers/{customerid}/orders/{orderid}")] public order getorderbycustomer(int customerid, int orderid) { ... }
you can set common prefix entire controller using [routeprefix]
attribute:
[routeprefix("api/books")] public class bookscontroller : apicontroller { // api/books [route("")] public ienumerable<book> get() { ... } // api/books/5 [route("{id:int}")] public book get(int id) { ... } }
you can visit this link more information on attribute routing in web api 2.
Comments
Post a Comment