为每个swagger ui请求添加头

c6ubokkw  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(189)

我正在配置我的swagger ui,如下所示:

builder.Services.AddSwaggerGen(c => {
    c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo()
    {
        Title = "Skillbased Middleware API Info",
        Version = "v1"
    });
});

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Company API Testing v1");
});

但是我不能向服务器发出一个signel crud请求,因为它总是需要一个特定的头(API版本)来通过。
我可以在哪里添加到我的swagger配置?这是一个asp.net核心Web应用程序
我可以这么说:

public class SwaggerHeader : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "ZUMO-API-VERSION=",
                Description = "3.0.0",
                Required = true // set to false if this is optional
            });
        }
    }

但是如果我试图执行任何命令,它只是加载和加载,从来没有得到一个结果...

uinbv5nw

uinbv5nw1#

我很接近:

public class SwaggerHeader : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "ZUMO-API-VERSION",
                In = ParameterLocation.Query,
                Required = true // set to false if this is optional
            });
        }
    }

现在在UI中选择不需要的调用。如ZUMMO所言:“3.0.0”和你的好去

相关问题