Reputation: 33
I have existing ASP.NET WebAPI which I want to migrate to ASP.NET Core WebAPI, but facing some issues in routing.
The APIs are already consumed by many clients and I cannot change the routes (or URLs). We have a controller (say ValuesController) with 2 Get methods but different parameters:
public async Task<IEnumerable<string>> Get(int xId, int yId)
public async Task<IEnumerable<string>> Get(int xId, int yId, int aId, int bId)
In current scenario (ASP.NET WebAPI), all the clients are able to call both methods on the basis of different parameters like:
For calling method first, one can request to URL:
http://localhost:4040/api/Values?xId=1&yId=2
and for second one, you can request to URL: http://localhost:4040/api/Values?xId=1&yId=2&aId=3&bId=4
. And it get automatically decided by the current routing available with ASP.NET WebAPI
But in case of ASP.NET Core, if I request to the same URL, it throws the Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException: Multiple actions matched
exception.
Any possible solution without changing the request call for every client application?
Upvotes: 1
Views: 287
Reputation: 1579
if is possible then why doont you make it a single function?? this is the way i think without disturbing other code i.e
public async Task<IEnumerable<string>> Get(int xId, int yId, int aId=0, int bId = 0)
{
if(aID>0 && bId>0)
{
//Your second controller code here
return your result
}
else
{
//Your second controller code here
return your result
}
}
now both the calls can be handled in same function
Upvotes: 0