Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/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
C# URI—;内版本的API版本不受支持;什么';怎么了?_C#_Rest_.net Core_Api Versioning - Fatal编程技术网

C# URI—;内版本的API版本不受支持;什么';怎么了?

C# URI—;内版本的API版本不受支持;什么';怎么了?,c#,rest,.net-core,api-versioning,C#,Rest,.net Core,Api Versioning,我有一个使用.net core 2.2的Web API项目(可能有问题) 这些路由正在等待OpenIdDict的OAuth授权,但这对我来说完全没问题。我正在尝试一种非常简单的方法: startup.cs仅包含: services.AddApiVersioning(); 出于测试目的,API控制器有三种不同的路由。请注意,控制器本身没有[Route()]或[ApiVersion()]注释 [HttpGet] [Authorize] [ApiVersion("2.0")] [Route("api

我有一个使用.net core 2.2的Web API项目(可能有问题)

这些路由正在等待OpenIdDict的OAuth授权,但这对我来说完全没问题。我正在尝试一种非常简单的方法:

startup.cs仅包含:

services.AddApiVersioning();
出于测试目的,API控制器有三种不同的路由。请注意,控制器本身没有[Route()]或[ApiVersion()]注释

[HttpGet]
[Authorize]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/vt")]
public IActionResult GetVt20()
{
    return Ok("2.0");
}

[HttpGet]
[Authorize]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/vt")]
public IActionResult GetVt10()
{
    return Ok("1.0");
}
如果我做了一个授权请求,比如

http://localhost:27713/api/v1.0/vt
.net core回答了一个错误的请求:

{"error":{"code":"UnsupportedApiVersion","message":"The HTTP resource that matches the request URI 'http://localhost:27713/api/v1.0/vt' does not support the API version '1.0'.","innerError":null}}

我缺少什么?

您使用的是最新版本的库吗?您使用的是端点路由还是传统路由?其余的配置是什么样子的?由于您提供的信息有限,我看不出它不起作用的直接原因

以下是一个基于您提供的信息的工作示例:

[ApiController]
public class VTController : ControllerBase
{
    [HttpGet]
    [ApiVersion( "1.0" )]
    [Route( "api/v{version:apiVersion}/[controller]" )]
    public IActionResult Get( ApiVersion apiVersion ) =>
        Ok( new { Action = nameof( Get ), Version = apiVersion.ToString() } );

    [HttpGet]
    [ApiVersion( "2.0" )]
    [Route( "api/v{version:apiVersion}/[controller]" )]
    public IActionResult GetV2( ApiVersion apiVersion ) =>
        Ok( new { Action = nameof( GetV2 ), Version = apiVersion.ToString() } );
}
以下路径按预期解决:

  • http://localhost/api/v1/vt
  • http://localhost/api/v2/vt

从2.1 NetCore迁移到2.2时,我遇到了同样的问题 只需添加到控制器类[ApiController]属性


你试过了吗@Daniel:试过了,也没用。你们都对@Chris Martinez提出了隐式解决方案,Lis指出了具体问题(我投票赞成并将Lis'作为解决方案。啊……我明白了。很抱歉你踩到了地雷@iquellis。很多人对从版本控制策略中删除UI控制器所需的操作不感兴趣。没有完美的答案。不幸的是,该解决方案导致了一些人的破坏行为。尽管它在do中被调用文档和发行说明,这很容易被忽略。wiki还描述了如果您想要旧的行为,如何更改默认规范。考虑到旧的行为,这并不重要,但无论如何还是要感谢。我们是新手。再次感谢兄弟。添加[ApiController]后方法不接受参数。在控制器上添加[ApiController]后,所有参数都为null。@MuhammadSaqib,您必须提供更多的上下文或可能是一个单独的问题。UI和API控制器之间确实没有区别,但可能是
[ApiController]
attribute导致某些UI功能在混合使用这两种功能时无法正常工作。你们都是对的。@Chris Martinez提出了隐式解决方案,Lis指出了具体问题(我赞成并将Lis设置为解决方案)。