api - API 版本控制问题 .NET Core 2.2 UnsupportedApiVersion
问题描述
我正在创建一个 API,需要在其中完成版本控制。我正在使用包 Microsoft.AspNetCore.Mvc.Versioning 3.1.3
我的 StartUp.cs 如下
在配置服务中
services.AddApiVersioning(o => {
o.ReportApiVersions = true;
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
});
services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
我想要完成版本控制的 2 个控制器如下
namespace JWTWithRefresh.Areas.V1.CMS
{
[AllowAnonymous]
[ApiVersion("1.0")]
[Route("api/[controller]/[action]")]
public class AppController : Controller
{
public IActionResult GetApp()
{
return Ok("This is from API V1");
}
}
}
另一个控制器如下
namespace JWTWithRefresh.Areas.V2.CMS
{
[AllowAnonymous]
[ApiVersion("2.0")]
[Route("api/[controller]/[action]")]
public class AppController : Controller
{
public IActionResult GetApp()
{
return Ok("This is from API V2");
}
}
}
我拨打电话时得到的响应如下
端点 = https://localhost:5001/api/App/GetApp?api-version=1.0
回应 =
{
"error": {
"code": "UnsupportedApiVersion",
"message": "The HTTP resource that matches the request URI 'https://localhost:5001/api/App/GetApp' is not supported.",
"innerError": null
}
}
如果有人经历过同样的问题,请指导我解决这个问题。
谢谢
解决方案
For anyone else having the problem, I solved it by following suggestion from LGSon in comments above:
Solution 1:
Add [ApiController] in Controller
Solution 2:
Disable API Behavior
services.AddApiVersioning( options => options.UseApiBehavior = false );
推荐阅读
- mysql - 在 powershell 中调用 MySql 数据库/表
- javascript - 如何使用 React 测试库获取当前的媒体查询值
- python - 如何在 tkinter 中添加第二行?
- docker - 构建 gcloud 项目时出现问题:复制失败:未指定源文件
- azure-api-management - 管理开发者门户所需的最少访问权限
- postgresql - 与多个 Postgresql 数据模式的单个连接。一些只读的
- python - Python:将目录添加到系统路径的上一级
- c# - 如何在 C# 中实现 UMAC 加密(RFC 4418)?
- javascript - 使用javascript以不同的间隔显示一个div
- apache-kafka - 如何在 IIDR CDC 中为 Kafka 主题创建派生列?