我已经在我的Core2.1API项目中成功地设置了API版本控制。
http://localhost:8088/api/Camps/ATL2016/speakers?api-version=x.x
1.1和2.0版本可以工作,但是1.0失败了,Get(string, bool)操作不明确。
ASP.NET核心网络服务器:
MyCodeCamp> fail: Microsoft.AspNetCore.Mvc.Routing.DefaultApiVersionRoutePolicy[1] MyCodeCamp> Request matched multiple actions resulting in ambiguity. Matching actions: MyCodeCamp.Controllers.Speakers2Controller.Get(string, bool) (MyCodeCamp) MyCodeCamp> MyCodeCamp.Controllers.SpeakersController.Get(string, bool) (MyCodeCamp) MyCodeCamp> fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] MyCodeCamp> An unhandled exception has occurred while executing the request. MyCodeCamp> Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
控制器Speakers2是用[ApiVersion("2.0")]装饰的,所以它的Get(string, bool)动作是2.0版本,那么为什么Versioning不能区分它们呢?
Microsoft.AspNetCore.Mvc.Versioning 3.0.0 (由于版本冲突无法安装更高的版本)
Startup.cs:
services.AddApiVersioning(cfg =>
{ cfg.DefaultApiVersion = new ApiVersion(1, 1);
cfg.AssumeDefaultVersionWhenUnspecified = true;
cfg.ReportApiVersions = true; });管制员:
[Route("api/camps/{moniker}/speakers")]
[ValidateModel]
[ApiVersion("1.0")]
[ApiVersion("1.1")]
public class SpeakersController : BaseController
{
. . .
[HttpGet]
[MapToApiVersion("1.0")]
public IActionResult Get(string moniker, bool includeTalks = false)
[HttpGet]
[MapToApiVersion("1.1")]
public virtual IActionResult GetWithCount(string moniker, bool includeTalks = false)
[Route("api/camps/{moniker}/speakers")]
[ApiVersion("2.0")]
public class Speakers2Controller : SpeakersController
{
...
public override IActionResult GetWithCount(string moniker, bool includeTalks = false)发布于 2019-01-13 18:02:46
显然,版本控制与多个Getxxx IActionResult混淆了。
我让它在Speakers controller virtual中运行,然后在Speakers2 controller中将其作为不被调用的占位符。我还必须只将[ApiVersion("2.0")]应用于GetWithCount action,而不是controller。
[Authorize]
[Route("api/camps/{moniker}/speakers")]
[ValidateModel]
[ApiVersion("1.0")]
[ApiVersion("1.1")]
public class SpeakersController : BaseController
[HttpGet]
[MapToApiVersion("1.0")]
[AllowAnonymous]
public virtual IActionResult Get(string moniker, bool includeTalks = false)
[Route("api/camps/{moniker}/speakers")]
public class Speakers2Controller : SpeakersController
public override IActionResult Get(string moniker, bool includeTalks = false)
{ return NotFound(); }
[ApiVersion("2.0")]
public override IActionResult GetWithCount(string moniker, bool includeTalks = false)发布于 2019-02-08 05:16:32
您以前的实现不起作用的原因是[ApiVersion]和[MapToApiVersion]是继承的而不是。这似乎有违直觉,但如果是这样,那么每个子类都会不断积累API版本。在第二个实现中,您没有覆盖原始的Get,因此它隐式地变成了控制器指定的2.0。这就是为什么您看到重复的原因,因为它现在在GetWithCount中是不明确的。
发布于 2021-08-24 10:17:35
找到了多个候选操作,但没有一个操作与请求的服务API版本“1”匹配。Soln:在操作方法上使用:>MapToApiVersion("1.0")属性
https://stackoverflow.com/questions/54166770
复制相似问题