[英]asp.net core swashbuckle how to use versioning in same controller
在 asp.net core 中使用带有 swashbuckle for swagger 的版本控制时,默认情况下不能在同一个控制器中使用多个方法,如下所示:
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("1")]
[ApiVersion("2")]
[ApiController]
public class TestController : ControllerBase
{
/// <summary>
/// test ver 1
/// </summary>
/// <returns></returns>
[HttpGet]
[MapToApiVersion("1")]
[ProducesResponseType(StatusCodes.Status200OK)]
public string Get()
{
return "Version 1";
}
/// <summary>
/// test ver 2
/// </summary>
/// <returns></returns>
[HttpGet]
[MapToApiVersion("2")]
[ProducesResponseType(StatusCodes.Status200OK)]
public string Get2()
{
return "Version 2";
}
}
When using versioning in asp.net core with swashbuckle for swagger, you cannot have by default multiple methods in same controller like that:在 asp.net core 中使用带有 swashbuckle for swagger 的版本控制时,默认情况下不能在同一个控制器中使用多个方法,如下所示:
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("1")]
[ApiVersion("2")]
[ApiController]
public class TestController : ControllerBase
{
/// <summary>
/// test ver 1
/// </summary>
/// <returns></returns>
[HttpGet]
[MapToApiVersion("1")]
[ProducesResponseType(StatusCodes.Status200OK)]
public string Get()
{
return "Version 1";
}
/// <summary>
/// test ver 2
/// </summary>
/// <returns></returns>
[HttpGet]
[MapToApiVersion("2")]
[ProducesResponseType(StatusCodes.Status200OK)]
public string Get2()
{
return "Version 2";
}
}
By default, you will have error message默认情况下,您将收到错误消息
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Conflicting method/path combination "GET v{version}/Test" for actions Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:冲突的方法/路径组合“GET v{version}/Test”用于操作
- ...Controllers.TestController.Get,...TestController.Get2 ...Controllers.TestController.Get,...TestController.Get2
because swagger-ui recognized both methods by same route因为 swagger-ui 通过相同的路线识别了这两种方法
localhost/v{version:apiVersion}/Test本地主机/v{version:apiVersion}/测试
Basic swashbuckle solution is to have two separate controllers for each version of method.基本的 swashbuckle 解决方案是为每个版本的方法使用两个单独的控制器。 That is bad solution for my use-case.这对我的用例来说是糟糕的解决方案。
对于ASP.Net core 5
及以上版本, 这在解决招摇文档问题,建议这样做,而不是编写自定义代码来生成特定版本的文档文章可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.