Core 2.1 APIVersioning Action 歧义

Core 2.1 APIVersioning Action ambiguity

我已经在我的 Core 2.1 API 项目中成功设置 API 版本控制。

http://localhost:8088/api/Camps/ATL2016/speakers?api-version=x.x

版本 1.12.0 有效,但 1.0 失败,Get(string, bool) 操作不明确。

ASP.NET 核心 Web 服务器:

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)

显然,版本控制与多个 Getxxx IActionResult 相混淆。

我在 Speakers controller virtual 中执行 Get 操作,然后在 Speakers2 中执行 overriding ] 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)

您之前的实施不起作用的原因是 [ApiVersion][MapToApiVersion] 不是 继承的。这似乎有悖常理,但如果确实如此,那么每个子类都会不断积累 API 个版本。在第二个实现中,您没有覆盖原来的 Get,所以它隐含地变成了控制器指定的 2.0。这就是您看到重复项的原因,因为它现在与 GetWithCount.

不明确

找到多个候选操作,但 none 与请求的服务 API 版本“1”匹配。 索恩: 在您的操作方法上使用:>[MapToApiVersion("1.0")] 属性