Jeremy Miller Jeremy Miller - 1 month ago 10
AngularJS Question

Why would the parameter name matter with Web Api id works while memberId ( both int) does not work

Just when after years of writing and consuming web api service methods, I am doing a project in which I was calling from Angular and then when the breakpoint in web api was NOT hit I was using postman.

I ended up tracing it down to the simple name of the signatures incoming parameter

This does NOT work

[HttpGet]
public IHttpActionResult GetActivityByMemberId(int memberId) {...}


"id" WORKS , WHY ??

[HttpGet]
public IHttpActionResult GetActivityByMemberId(int id) {...}


Postman & Angular call

http://localhost:49810/umbraco/api/activityapi/GetActivityByMemberId/2


I'm a bit lost as to why I would have to change parameter name to "id" when I do not even specify that in Postman (testing) or Angular
$http.get
...

Help me understand! Please !

Answer

It looks like a routing parameter matching issue. Most probably you are using convention-based routing and the routeTemplate has something like

// Convention-based routing.
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

It is matching the parameter names in the route template to those in the controller actions.

read up on Routing and Action Selection in ASP.NET Web API to get a better understanding of how routing works

Comments