c# - Core 2.1 APIVersioning 动作歧义
问题描述
我已经在我的 Core 2.1 API 项目中成功设置了 API 版本控制。
http://localhost:8088/api/Camps/ATL2016/speakers?api-version=x.x
版本1.1
和2.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
(由于版本冲突,无法安装更高版本)
启动.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
显然版本控制与多个s混淆了。
我通过在 the 中进行Get
操作,Speakers
controller
virtual
然后overriding
将其Speakers2
controller
作为不会被调用的占位符来使其工作。我还必须将[ApiVersion("2.0")]
only 应用于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)
推荐阅读
- java - 单链表删除方法不识别连接字符串参数?
- python - Python - 编写一个函数,其中包含 x 个子列表,每个子列表中有 y 个元素,每个元素等于 z
- javascript - Ajax CallBack 没有时间触发
- android-studio - 如何在 Android Studio 的 Textviews 中显示来自 SQLite 查询对象的数据
- python - 计算 MAPE 时,带有网格搜索的 FBProphet 会引发错误
- pandas - 在 Python 中的地图上绘制方式(线串)
- generics - 将 Rust 泛型约束到自定义数据类型
- jenkins - Jenkins 构建历史消失了
- function - 根据喜好获取号码
- android - 使用从后端获取的图像在运行时更改徽标应用程序移动设备