我已经在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 Core 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混淆。

我通过在Get Speakers controller中执行virtual操作,然后在overriding Speakers2中将其作为不被调用的占位符来使其起作用。我还必须仅将controller应用于[ApiVersion("2.0")] GetWithCount而不是action

[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)

关于c# - Core 2.1 APIVersioning Action 含糊,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54166770/

10-16 23:57