gpt4 book ai didi

c# - aspnet-api-版本控制 : Why api returns 200 (ok) if wrong Api version is provided in the call to api

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:21 26 4
gpt4 key购买 nike

以下是使用的 NuGet 版本。

PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.3.0" PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="2.2.0" PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0"

startup.cs

 services.AddApiVersioning(o =>
{
o.ReportApiVersions = true;
o.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
o.AssumeDefaultVersionWhenUnspecified = true;
o.ErrorResponses = new DefaultErrorResponseProvider();
});

DemoTestController.cs

   [ApiVersion("2.0")]
[ApiController]
[Route("api/v{version:apiVersion}/[Controller]")]
public class DemoTest : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new JsonResult(new { ResourceName = "DemoTestAPIController Version 2" });
}
}

对于上面的 Controller

http://localhost:53858/api/v2.0/DemoTest (此 URL 有效,其响应应为 200,并且按预期工作)

但是对于下面的 URL,响应应该是 400(Forbidden)。 http://localhost:53858/api/v1.0/DemoTest

http://localhost:53858/api/v3.0/DemoTest

以下是各个 api versoinig 案例的预期响应。 Error Response https://github.com/Microsoft/aspnet-api-versioning/wiki/Error-Responses

尝试到现在:我还尝试用 MyErrorResponseProvider 覆盖 DefaultError Response,但调试器根本没有命中它。

借助这个asnswer

services.AddApiVersioning(o =>
{
o.ReportApiVersions = true;
o.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
o.AssumeDefaultVersionWhenUnspecified = true;
o.ErrorResponses = new MyErrorResponseProvider();
});

MyErrorResponseProvider

class MyErrorResponseProvider : DefaultErrorResponseProvider
{
// note: in Web API the response type is HttpResponseMessage
public override IActionResult CreateResponse( ErrorResponseContext context )
{
switch ( context.ErrorCode )
{
case "UnsupportedApiVersion":
context = new ErrorResponseContext(
context.Request,
context.StatusCode,
context.ErrorCode,
"My custom error message.",
context.MessageDetail );
break;
}

return base.CreateResponse( context );
}
}

最佳答案

下面的代码适合我

我使用的是 3.1.6 版本

<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.6" />

我的startup.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddApiVersioning(options =>
{
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

我的 Controller

[ApiVersion("1.0")]
[ApiExplorerSettings(GroupName = "v1")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class ValuesController : ControllerBase

如果我尝试使用 v2,我会得到这个错误

error: {
code: "UnsupportedApiVersion",
message: "The HTTP resource that matches the request URI 'http://localhost:61273/api/v2/values' does not support the API version '2'.",
innerError: null
}

关于c# - aspnet-api-版本控制 : Why api returns 200 (ok) if wrong Api version is provided in the call to api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58392495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com