首页 > 解决方案 > 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
    }
}

如果有人经历过同样的问题,请指导我解决这个问题。

谢谢

标签: apiasp.net-coreasp.net-core-2.0asp.net-core-webapi

解决方案


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

推荐阅读