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); } }
route order matters. generic (
defaultapi) route has declared last since it's "greedy" - otherwise catch requests, preventing other routes kicking inyour 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
Post a Comment