c# - Defining Web API routes -


i'm exposing functionality through web api, , urls want expose example:

  • /api/organizations (returns list of organizations (get))
  • /api/organizations?$top=2 (returns list of organizations filtered odata (get))
  • /api/organizations/2 (returns details of organization id 2 (get))
  • /api/organizations/addinstitution (adds new institution organization (post))
  • /api/organizations/removeinstitution (removes institution (post))

in order that, had change routing in webapiconfig class like:

config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{id}",             defaults: new { id = routeparameter.optional },             constraints: new { id = @"\d*" }         );   config.routes.maphttproute(             name: "specificactionsroute",             routetemplate: "api/{controller}/{action}/{id}",             defaults: new { id = routeparameter.optional }         );      

this seems work, wondering if optimal way of defining routes. need 2 routes? there maybe better way?

edit: complete, have use attribute routing like:

[get("organizations")] public iqueryable<organizationsummaryviewmodel> get(odataqueryoptions<organization> odataquery) { }  [get("organizations/{id}")] public organizationdetailsviewmodel get(int id) { }  [post("organizations/addinstitutiontoorganization")] public addinstitutiontoorganizationcommandresult addinstitutiontoorganization(addinstitutiontoorganizationcommand command) { }  [post("organizations/anotheroperation")] public anotheroperationcommandresult anotheroperation(anotheroperationcommand command) { } 

edit 2: ended removing attribute routes, , kept 2 templated routes in webapiconfig.

your configuration best way define routes urls plan on handling. 2 routes cannot consolidated because first route handles two-segment urls assigning second segment id, whereas second route handling three-segment urls assigns second segment action. necessary have 2 different routes because assigning values differently based on whether there 2 or 3 segments url.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -