Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
API版本控制.NET Core 2.2不支持的版本问题_Api_Asp.net Core_Asp.net Core 2.0_Asp.net Core Webapi - Fatal编程技术网

API版本控制.NET Core 2.2不支持的版本问题

API版本控制.NET Core 2.2不支持的版本问题,api,asp.net-core,asp.net-core-2.0,asp.net-core-webapi,Api,Asp.net Core,Asp.net Core 2.0,Asp.net Core Webapi,我正在创建一个API,需要在其中进行版本控制。我正在使用Microsoft.AspNetCore.Mvc.Versioning 3.1.3包 我的StartUp.cs如下 在配置服务中 services.AddApiVersioning(o => { o.ReportApiVersions = true; o.AssumeDefaultVersionWhenUnspecified = true; o.DefaultAp

我正在创建一个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);
我想对其进行版本控制的两个控制器如下

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");
        }
    }
}
我打电话时得到的答复如下

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");
        }
    }
}
端点=

答复=

{
    "error": {
             "code": "UnsupportedApiVersion",
             "message": "The HTTP resource that matches the request URI 'https://localhost:5001/api/App/GetApp' is not supported.",
             "innerError": null
    }
}
如果有人经历过相同的问题,请指导我解决此问题


谢谢

像这样更改路由属性

[Route("v{version:apiVersion}/[controller]/[action]")]
和端点=或


更多信息:

对于其他有问题的人,我通过上面评论中LGSon的建议解决了这个问题:

解决方案1:

Add [ApiController] in Controller
解决方案2:

禁用API行为

services.AddApiVersioning( options => options.UseApiBehavior = false );

看看这里,看看这是否解决了它:谢谢你击中了靶心。解决了我的问题。标记为已回答