0
votes

I have a Web API that I need to configure multiple route (currently using convention-based) but it looks like the first route is grabbing every request.

Here are the routes from WebApiConfig.cs

config.Routes.MapHttpRoute(
    name: "MyProtApi",
    routeTemplate: "api/{controller}/{action}/{myprotocolsid}",
    defaults: new { myprotocolsid = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
    name: "AssessmentApi",
    routeTemplate: "api/{controller}/{action}/{assessmentid}",
    defaults: new { assessmentid = RouteParameter.Optional }
);

Whats working:

  • Any calls to the "MyProtocols" controller whether there is an "myprotocolsid" provided or not
    • /api/myprotocols/getbyid/642ff9cd-fb32-4a79-aaa4-088278796bb0
    • public MyProtocolsViewModel GetById(Guid myProtocolsId)
  • Calls to the "Assessment" controller that don't provide an "assessmentid"
    • /api/assessment/list
    • public IEnumerable List()

Whats not working

  • Calls to the "Assessment" controller that have an "assessmentid" parameter
    • /api/assessment/getbyid/8ba32ce6-8a97-4211-b649-afcc8e194f21
    • public AssessmentTreeViewModel GetById(Guid assessmentId)
    • "No action was found on the controller 'Assessment' that matches the request."
1

1 Answers

0
votes

Here is what I've found. I commented out the second route and found that the behavior did not change at all. Obviously, this indicates that the first route is catching every request.

Upon further thought, that makes sense due to the fact that in the URI, the myProtocolsId and assessmentId are indistinguishable from one another so, in retrospect, it makes sense that the first route is handling all of these requests.

My take is that there are 2 alternatives; either using a more generic parameter name ("id") or to force a named parameter that is distinguished by names query string values /api/assessment/getbyid?assessmentid=foo. I have opted for the former and have things working the way I want.