简体   繁体   English

Swashbuckle + ASP.Net Core WebApi:Swagger 文档不包含用于版本选择的 Request-Header 或 QueryParameter?

[英]Swashbuckle + ASP.Net Core WebApi: Swagger Document does not include Request-Header or QueryParameter for version selection?

I use ASP.Net Core WebApi, Swashbuckle and Microsoft.AspNetCore.Mvc.Versioning to document and versioning my API.我使用 ASP.Net Core WebApi、Swashbuckle 和 Microsoft.AspNetCore.Mvc.Versioning 来记录和控制我的 API。

The versioning also works so far.到目前为止,版本控制也有效。

My Problem:我的问题:

The generated Swagger UI Document does not include parameters (Request-Header or Query Parameter) to determine the version of the Endpoint.生成的 Swagger UI 文档不包含用于确定 Endpoint 版本的参数(Request-Header 或 Query Parameter)。 So when i press "Execute" in the Swagger-Document the wrong version (default version) is picked for the endpoint.因此,当我在 Swagger-Document 中按“执行”时,会为端点选择错误的版本(默认版本)。

To be precise:准确地说:

Swagger executes this request: https://localhost:5001/values Swagger 执行这个请求:https://localhost:5001/values

But, it should execute this requerst: https://localhost:5001/values?api-version=2.0但是,它应该执行这个请求:https://localhost:5001/values?api-version=2.0

在此处输入图片说明

Code:代码:

Controller:控制器:

[ApiController]
[Route("[controller]")]
[SwaggerTag("Gets some values. Have fun with it")]
[ApiVersion("1.0")]
[ApiVersion("2.0")]
public class ValuesController : ControllerBase
{
    public ValuesController()
    {
    }

  /// <summary>
  /// Gets all values
  /// </summary>
  /// <remarks>There are values from 1 to 10</remarks>
  /// <returns></returns>
  [HttpGet]
  [SwaggerResponse(200, "Request was successful a list of values was returned", typeof(int[]))]
  [MapToApiVersion("1.0")]
  public async Task<IActionResult> Get()
  {
        return Ok(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  }

  /// <summary>
  /// Gets all values
  /// </summary>
  /// <remarks>There are values from 1 to 20</remarks>
  /// <returns></returns>
  [HttpGet]
  [SwaggerOperation(Tags = new[] { "Values", "Changed Endpoints" })]
  [SwaggerResponse(200, "Request was successful a list of values was returned", typeof(int[]))]
  [MapToApiVersion("2.0")]
  public async Task<IActionResult> Getv2()
  {
        return Ok(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 });
  }

Enable versioning:启用版本控制:

        services.AddApiVersioning(config =>
        {
            config.DefaultApiVersion = new ApiVersion(1, 0);
            config.AssumeDefaultVersionWhenUnspecified = true;
            config.ReportApiVersions = true;

            config.ApiVersionReader = ApiVersionReader.Combine(new QueryStringApiVersionReader(),
             new HeaderApiVersionReader()
             {
                 HeaderNames = { "x-api-version" }
             });
        });

Enable SwaggerGen:启用 SwaggerGen:

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("1.0", new OpenApiInfo
            {
                Title = "API v1.0",
                Version = "1.0",
            });
            c.SwaggerDoc("2.0", new OpenApiInfo
            {
                Title = "API v1.0",
                Version = "1.0",
            });
            c.EnableAnnotations();
            c.IncludeXmlComments(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "OpenApi.xml"));
            c.DocInclusionPredicate((docName, apiDesc) =>
            {
                if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)) return false;

                var versions = methodInfo.GetCustomAttributes(true)
                    .OfType<Microsoft.AspNetCore.Mvc.MapToApiVersionAttribute>()
                    .SelectMany(attr => attr.Versions).ToList();

                return versions.Any(v => v.ToString() == docName);
            });
        });

Can someone help me?有人能帮我吗?

Solved this by adding:通过添加解决了这个问题:

     services.AddVersionedApiExplorer(options =>
        {
            options.GroupNameFormat = "'v'VVV";
            options.DefaultApiVersion = new ApiVersion(1, 0);
            options.AssumeDefaultVersionWhenUnspecified = true;
            options.DefaultApiVersionParameterDescription = "Do NOT modify api-version!";
        });

The parameters are now added.现在已添加参数。 But unfortunately no default value is filled automatically.但不幸的是,没有自动填充默认值。 Anyone have an idea?有人有想法吗?

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core - Swashbuckle 未创建 swagger.json 文件 - ASP.NET Core - Swashbuckle not creating swagger.json file 使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型 - Rename model in Swashbuckle 6 (Swagger) with ASP.NET Core Web API Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义” - Swashbuckle/Swagger + ASP.Net Core: "Failed to load API definition" 如何配置Swagger / Swashbuckle自定义序列化程序IControllerConfiguration ASP.NET WebAPI - How to configure Swagger/Swashbuckle custom serializer IControllerConfiguration ASP.NET WebAPI 可以从Swashbuckle / Swagger UI中排除ASP.NET WebAPI操作参数吗? - Can an ASP.NET WebAPI action parameter be excluded from Swashbuckle/Swagger UI? 使用ASP.NET MVC WebApi的Swashbuckle 5.4.0 - 在swagger网页中没有显示任何文档 - Swashbuckle 5.4.0 with ASP.NET MVC WebApi - No documentation is shown inside the swagger webpage 如何在 ASP.NET Core Swagger (Swashbuckle.AspNetCore) 中定义控制器描述? - How to define controller descriptions in ASP.NET Core Swagger (Swashbuckle.AspNetCore)? 如何大摇大摆地记录一个直接读取 Request.Body 的 ASP.NET 核心操作 - How to swagger-document an ASP.NET Core action which reads Request.Body directly 如何在 ASP.NET Core 中的 Swagger 中包含 XML 注释文件 - How to include XML comments files in Swagger in ASP.NET Core 从标头Asp.Net Core WebApi绑定自定义对象 - Binding Custom Object From Header Asp.Net Core WebApi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM