gpt4 book ai didi

c# - 在 Swagger for ASP .NET Core 3.1 中为标题添加过滤器

转载 作者:行者123 更新时间:2023-12-03 21:59:32 24 4
gpt4 key购买 nike

我遵循了这个:
Web Api How to add a Header parameter for all API in Swagger
还有这个:
How to send custom headers with requests in Swagger UI?
但是,这些 IParameter、Parameter 或 NonBodyParameters 均不适用于 ASP .NET CORE 3.1。
我想在我的 Swagger 中添加一个标题,该标题采用一个租户 ID,该 ID 最好取自登录用户。
我也经历过这个:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore
谁能指出我正确的方向?

using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.OpenApi.Models;

namespace Intent2.Auth.Utils
{
public class AddRequiredHeaderParameter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{

if (operation.Parameters == null)
operation.Parameters = new List<IParameter>();

operation.Parameters.Add(new NonBodyParameter
{
Name = "X-User-Token",
In = "header",
Type = "string",
Required = false
});
}
}
}
services.AddSwaggerGen(options =>
{
options.OperationFilter<AddRequiredHeaderParameter>();
}

最佳答案

随着与 ASP.NET Core 3.1 兼容的最新版本的 Swashbuckle,许多类型已被 Microsoft.OpenApi.Models 中的等效类型替换。命名空间。所以你不应该再使用像 NonBodyParameter 这样的类型。或 IParameter .这两个都被一个类 OpenApiParameter 替换了。 .
您的代码应如下所示

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;

namespace Intent2.Auth.Utils
{
public class AddRequiredHeaderParameter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{

if (operation.Parameters == null)
operation.Parameters = new List<OpenApiParameter>();

operation.Parameters.Add(new OpenApiParameter()
{
Name = "X-User-Token",
Description = "Access Token",
In = ParameterLocation.Header,
Schema = new OpenApiSchema() { Type = "String" },
Required = true,
Example = new OpenApiString("Tenant ID example")
});
}
}
}
然后在你的启动中,像往常一样简单地注入(inject) SwaggerGen
services.AddSwaggerGen(options =>
{
options.OperationFilter<AddRequiredHeaderParameter>();
}
例如,您甚至可以使租户 ID 像配置文件一样来自外部。为此,请修改您的 AddRequiredHeaderParameter如下
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;

namespace Intent2.Auth.Utils
{
public class AddRequiredHeaderParameter : IOperationFilter
{
private string _tenantIdExample;

public AddRequiredHeaderParameter(string tenantIdExample)
{
if (string.IsNullOrEmpty(tenantIdExample ))
throw new ArgumentNullException(nameof(tenantIdExample ));

_tenantIdExample = tenantIdExample;
}

public void Apply(OpenApiOperation operation, OperationFilterContext context)
{

if (operation.Parameters == null)
operation.Parameters = new List<OpenApiParameter>();

operation.Parameters.Add(new OpenApiParameter()
{
Name = "X-User-Token",
Description = "Access Token",
In = ParameterLocation.Header,
Schema = new OpenApiSchema() { Type = "String" },
Required = true,
Example = new OpenApiString(_tenantIdExample)
});
}
}
}
从你的创业公司就这样称呼它
services.AddSwaggerGen(options =>
{
options.OperationFilter<AddRequiredHeaderParameter>("Tenant ID example");
}
顺便说一句,如果你的类(class)被称为 AddRequiredHeaderParameter你应该实际设置 Required = true而不是 false

关于c# - 在 Swagger for ASP .NET Core 3.1 中为标题添加过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60522015/

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