【问题标题】:Swagger - hide api version parametersSwagger - 隐藏 api 版本参数
【发布时间】:2020-03-21 19:28:29
【问题描述】:

是否可以隐藏“api-version”和“x-api-version”参数?

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

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

        services.AddVersionedApiExplorer(
            options =>
            {
                // note: the specified format code will format the version as "'v'major[.minor][-status]"
                options.GroupNameFormat = "'v'VVV";

                options.DefaultApiVersionParameterDescription = "Do NOT modify api-version!";
            });

我已经检查了实现“RemoveVersionFromParameter”方法的how-to-set-up-swashbuckle-vs-microsoft-aspnetcore-mvc-versioning,但在这种情况下,Swagger 页面会丢失 api 版本并始终使用默认的 v1.0。如代码sn-p所示,我使用的是QueryStringApiVersionReader和HeaderApiVersionReader,但是我不想支持url api版本。

注意:API 确实为所有版本(例如 V1、V1.1、V2.0)提供了多个 swagger json 页面

【问题讨论】:

    标签: c# .net api swagger-ui swashbuckle


    【解决方案1】:

    您可以尝试操作过滤器。这和 Helder 的方案类似,但是实现不一定要在文档级别,所以看起来更简单:

    public void Configure(SwaggerGenOptions options)
    {
        // Filter out `api-version` parameters globally
        options.OperationFilter<ApiVersionFilter>();
    }
    
    internal class ApiVersionFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            var parametersToRemove = operation.Parameters.Where(x => x.Name == "api-version").ToList();
            foreach (var parameter in parametersToRemove)
                operation.Parameters.Remove(parameter);
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可以add your own custom CSS 并使用它来隐藏这些元素(并进行您想要的任何其他自定义)。

      app.UseSwaggerUI(c =>
      {
          ...
          c.InjectStylesheet("/swagger-ui/custom.css");
          ...
      });
      

      编辑 - 示例:

      假设你想隐藏——在我的例子中;您可以轻松地将其调整为适合您的 - 此“删除篮子”操作中的 tenantId 参数:

      这样可以:

      div#operations-baskets-remove tr[data-param-name="tenantId"] {
          display: none;
      }
      

      【讨论】:

      • 您能否解释一下我如何隐藏这些参数,因为在 html 中,参数没有 ID,例如: ?
      • @Odrai - 有很多 ID;您不需要在要隐藏的确切元素上使用 ID。使用 CSS 的“C”部分,向上查找有用的 ID,然后级联到您想要的元素。我在答案中添加了一个示例。
      【解决方案3】:

      您是否查看过IDocumentFilter,您可以从最终的 swagger.json 中删除内容,然后将其从 UI 中删除

      这是我从定义中删除一些属性的示例:

          private class HideStuffDocumentFilter : IDocumentFilter
          {
              public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry s, IApiExplorer a)
              {
                  foreach (var definition in swaggerDoc.definitions)
                  {
                      foreach (var prop in definition.Value.properties.ToList())
                      {
                          if (prop.Value.maxLength == 9999)
                              definition.Value.properties.Remove(prop);
                      }
                  }
              }
          }
      

      我这里还有一些示例:
      https://github.com/heldersepu/Swagger-Net-Test/blob/e701b1d20d0b42c1287c3da2641ca521a0a7b592/Swagger_Test/App_Start/SwaggerConfig.cs#L766

      【讨论】:

        【解决方案4】:

        这可以通过设置 ApiExplorerOption SubstituteApiVersionInUrl = true 来完成。在你的情况下:

            services.AddVersionedApiExplorer(
                options =>
                {
                    // note: the specified format code will format the version as "'v'major[.minor][-status]"
                    options.GroupNameFormat = "'v'VVV";
        
                    options.DefaultApiVersionParameterDescription = "Do NOT modify api-version!";
                    options.SubstituteApiVersionInUrl = true;
                });
        

        【讨论】:

          【解决方案5】:

          您可以添加 startup.cs 文件。

          services.AddApiVersioning(options =>
                  {
                      // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
                      options.ReportApiVersions = true;
                  });
          
          
          services.AddVersionedApiExplorer(options =>
                  {
                      // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
                      // note: the specified format code will format the version as "'v'major[.minor][-status]"
                      options.GroupNameFormat = "'v'VVV";
          
                      // note: this option is only necessary when versioning by url segment. the SubstitutionFormat
                      // can also be used to control the format of the API version in route templates
                      options.SubstituteApiVersionInUrl = true;
                  });
          

          然后,您可以在控制器上添加顶部。但是我尝试没有这个([ApiVersion(“1.0”)]),它可以运行。我已经成功隐藏版本参数。

          [ApiVersion("1.0")]
          [Route("api/v{version:apiVersion}/Check")]
          [ApiController]
          [Authorize]
          public class CheckController : ControllerBase {}
          

          【讨论】:

            猜你喜欢
            相关资源
            最近更新 更多
            热门标签