c# - Calling custom methods in API controller -
c# - Calling custom methods in API controller -
i added method in web api controller:
public string getpeopleinfamily(string familyid)
i followed examples question: custom method names in asp.net web api
i updated routeconfig.cs file based off of read above: public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}");
routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { id = urlparameter.optional } ); routes.maphttproute("defaultapiwithid", "api/{controller}/{id}", new { id = routeparameter.optional }, new { id = @"\d+" }); routes.maphttproute("defaultapiwithaction", "api/{controller}/{action}"); routes.maphttproute("defaultapiwithactionandid", "api/{controller}/{action}/{id}", new { id = routeparameter.optional }, new { id = @"\d+" }); routes.maphttproute("defaultapiget", "api/{controller}", new { action = "get" }, new { httpmethod = new httpmethodconstraint("get") }); routes.maphttproute("defaultapipost", "api/{controller}", new { action = "post" }, new { httpmethod = new httpmethodconstraint("post") }); }
but i'm still getting 404 when calling method:
request /api/people/getpeopleinfamily/101 http/1.1
{"message":"no http resource found matches request uri 'http://localhost:17438/api/people/getpeopleinfamily/101 '.","messagedetail":"no action found on controller 'people' matches request."}
what need work? as, best practice add together custom methods homecoming more complicated info set?
the familyid
not mapped in route. can create new mapping wich adds familyid id mapping or rewrite parameter of getpeopleinfamily
id
.
rewrite:
public string getpeopleinfamily(string id)
create new route: when create new route sure remove mapping id
otherwise won't work.
routes.maphttproute("defaultapiwithactionandfamilyid", "api/{controller}/{action}/{familyid}", new { familyid = routeparameter.optional }, new { familyid = @"\d+" });
c# asp.net asp.net-web-api asp.net-web-api2 asp.net-web-api-routing
Comments
Post a Comment