c# - Web Api 2 MapHttpRoute is not working and is forcing me to use query strings -


hi have customs config.routes.maphttproute api forcing me use query string parameter instead regular parameters separated / such has ...api/data/2/23.

if call api ...api/collectdata/1 not work if call works ...api/collectdata?researchid=1

this have in webapiconfig

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

and controller looks this:

public ihttpactionresult collectdata(int researchid)           {               try               {                   service.savedatabyresearchid(researchid);                   return ok(new { message = "data collected , saved" });               }               catch (exception e)               {                   return new customerror(e.message,request);               }           } 

  1. route order matters. generic (defaultapi) route has declared last since it's "greedy" - otherwise catch requests, preventing other routes kicking in

  2. your specific route doesn't have controller defined, need modify to:

        config.routes.maphttproute(         name: "collectdatafrompets",         routetemplate: "api/collectdata/{researchid}",         defaults: new {controller = "collectdata"} //or whatever controller name     ); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -