c# - Basic Conventional routing asp.net webapi -


i have simple query on asp.net web api routing. have following controller:

public class customerscontroller: apicontroller {     public list<someclass> get(string searchterm)     {          if(string.isnullorempty(searchterm))          {              //return complete list          }          else          {              //return list.where (customer name contains searchterm)          }     } } 

my routing configuration (conventional) looks this:

config.routes.maphttproute(name:"defaultapi", routetemplate:"api/{controller}/{id}" defaults:new {id = routeparameter.optional});  config.routes.maphttproute(name:"customersapi", routetemplate:"api/{controller}/{searchterm}" defaults:new {searchterm = routeparameter.optional}); 

if hit url: http://localhost:57169/api/customers/vi 404-not found

if reverse order of routes, works. question in first case, matching first route (defaultapi)? if not, why not trying second route?

this route template

config.routes.maphttproute(name:"defaultapi",     routetemplate:"api/{controller}/{id}",     defaults:new {id = routeparameter.optional} ); 

match url because id can type : string, int etc. url respect template , route chosen.

to make template more restrcitive , make asp.net web api go next template need add constraints saying "id parameter must numeric type template". add constraints parameter below:

config.routes.maphttproute(name:"defaultapi",     routetemplate: "api/{controller}/{id}",     defaults: new {id = routeparameter.required}, // <- here put required make sure when user doesn't give searchterm template not chosen.     constraints: new {id = @"\d+"} // <- regular expression used id must numeric value template. ); 

so using url http://localhost:57169/api/customers/vi above template skipped , next chosen.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -