Jeremy Miller Jeremy Miller - 11 months ago 53
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

public IHttpActionResult GetActivityByMemberId(int memberId) {...}

"id" WORKS , WHY ??

public IHttpActionResult GetActivityByMemberId(int id) {...}

Postman & Angular call


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

Help me understand! Please !

Answer Source

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.
    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