简体   繁体   中英

Routing migration from ASP.NET WebAPI to ASP.NET Core WebAPI, ambiguous method issue

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?

if is possible then why doont you make it a single function?? this is the way i think without disturbing other code ie

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM