gpt4 book ai didi

c# - Core 2.1 APIVersioning Action 歧义

转载 作者:行者123 更新时间:2023-11-30 22:56:45 27 4
gpt4 key购买 nike

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

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

版本 1.12.0 工作,但 1.0 失败并在 Get(string, bool) 上出现歧义 Action 。

ASP.NET 核心网络服务器:

MyCodeCamp> 失败:Microsoft.AspNetCore.Mvc.Routing.DefaultApiVersionRoutePolicy[1]
MyCodeCamp> 请求匹配多个 Action 导致歧义。匹配 Action :
MyCodeCamp.Controllers.Speakers2Controller.Get(字符串, bool) (MyCodeCamp)
MyCodeCamp> MyCodeCamp.Controllers.SpeakersController.Get(string, bool) (我的代码营)
MyCodeCamp> 失败:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
MyCodeCamp> 执行请求时发生未处理的异常。
MyCodeCamp> Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException: 多个操作
匹配。以下操作匹配路由数据并满足所有约束:

Controller Speakers2 装饰有 [ApiVersion("2.0")] 所以它的 Get(string, bool) action 是版本 2.0 所以为什么 Versioning 不能区分它们?

Microsoft.AspNetCore.Mvc.Versioning 3.0.0(由于版本冲突无法安装更高版本)

启动.cs:

  services.AddApiVersioning(cfg =>
{ cfg.DefaultApiVersion = new ApiVersion(1, 1);
cfg.AssumeDefaultVersionWhenUnspecified = true;
cfg.ReportApiVersions = true; });

Controller :

  [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 操作,然后 overriding 它在 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)

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com