asp.net mvc 4 - Web api routing methods with different parameter names -


this question have been answered hundred times, couldnt find proper resource. in webapi project (default project provided vs) have valuescontroller below.

   public string get(int id)     {         return "value";     }      [httpget]     public string findbyname(string name)     {         return name;     }      [httpget]     public string findbyid(int id)     {         return id.tostring();     } 

in webapiconfig.cs, have following route mapping.

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

now findbyid() action working when try in browser. why rest of api calls return "no http resource found matches request"

how can 3 methods working? without using attributerouting. lack of basic concepts of web api? ( think yes)

your actionapibyid route matches actionapi route, id integer try using constraint this.

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

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -