0
votes

I'm using Web Api to create REST APIs that have parameters ranging from 0 to 4. Below are examples of my APIs :

  1. GetTaxRates()
  2. GetTaxRatesByDate(string date, string name= "") // name is an optional parameter
  3. GetTaxBetweenDates(string frmDate, string toDate, string name="") // name is an optional parameter
  4. GetRecentTaxRates(string name= "") // name is an optional parameter

For these different GET calls, i have created the following routes in the webapiconfig :

  1. routeTemplate: "api/{controller}/{action}", defaults: new { }

  2. routeTemplate: "api/{controller}/{action}/{date}/{name}", defaults: new { name= RouteParameter.Optional }

  3. routeTemplate: "api/{controller}/{action}/{frmdate}/{toDate}/{name}", defaults: new { name= RouteParameter.Optional }

When i am calling the APIs, it works fine for most of the routes, but there seems to be a conflict between the routes and i get the error "No action was found on the controller 'TaxRate' that matches the request." when i call the action api/TaxRate/GetTaxBetweenDates/2015-04-01/201504-10

Although i am getting the results when calling the same api with all the parameters: api/TaxRate/GetTaxBetweenDates/2015-04-01/201504-10/abc

When i change the sequence of the routes, the same issue occurs GetTaxRatesbyDate API call.

2

2 Answers

0
votes

As per rule of thumb in web api routing you should register the more specific routes first. Have a look at this link Routing order. What might help here is registering more specific routes like "api/{controller}/GetTaxRatesByDate/{date}/{name}", defaults: new { name= RouteParameter.Optional }

-1
votes

I was able to resolve the above issue using attribute routing.