繁体   English   中英

从ASP.NET WebAPI到ASP.NET Core WebAPI的路由迁移,方法问题不明确

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

我已经有想要迁移到ASP.NET Core WebAPI的现有ASP.NET WebAPI,但是在路由中遇到了一些问题。

这些API已被许多客户端占用,并且我无法更改路由(或URL)。 我们有一个具有2个Get方法但参数不同的控制器(例如ValuesController):

public async Task<IEnumerable<string>> Get(int xId, int yId)

public async Task<IEnumerable<string>> Get(int xId, int yId, int aId, int bId)

在当前方案(ASP.NET WebAPI)中,所有客户端都可以根据不同的参数来调用这两种方法,例如:

对于第一种调用方法,可以请求URL: http://localhost:4040/api/Values?xId=1&yId=2 ;对于第二种调用方法,可以请求URL: http://localhost:4040/api/Values?xId=1&yId=2&aId=3&bId=4 它由ASP.NET WebAPI可用的当前路由自动决定

但是对于ASP.NET Core,如果我请求相同的URL,它将引发Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException: Multiple actions matched异常Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException: Multiple actions matched

没有更改每个客户端应用程序的请求调用的任何可能解决方案?

如果可能的话,为什么不将其设为单个功能? 这是我的想法,而不会打扰其他代码,即

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

现在两个调用可以在同一函数中处理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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