小编JVI*_*VIH的帖子

如何使用 C# .NET CORE 在 NSwag 文档中添加自定义标题?

我需要添加自定义标题,但无法弄清楚。我正在尝试使用新的 services.AddOpenApiDocument() 而不是 services.AddSwaggerDocument()。我想在我的整个 API 上添加这些自定义标头,而不仅仅是单个方法或控制器。我试图添加一个操作处理器,但是当我加载 swagger UI 时,我收到以下错误“无法呈现此组件,请参阅控制台。”

这是我的代码片段ConfigureServices():

    services.AddOpenApiDocument(document =>
    {
        ...
        // this works fine
        document.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer"));
        document.DocumentProcessors.Add(new SecurityDefinitionAppender("Bearer", new SwaggerSecurityScheme
            {
                Type = SwaggerSecuritySchemeType.ApiKey,
                Name = "Authorization",
                In = SwaggerSecurityApiKeyLocation.Header
            })
        );

        // this is the header i want to show up for all endpoints that is breaking
        document.OperationProcessors.Add(new SampleHeaderOperationProcessor());
    });
Run Code Online (Sandbox Code Playgroud)

这是我的操作处理器:

public class SampleHeaderOperationProcessor : IOperationProcessor
{
    public Task<bool> ProcessAsync(OperationProcessorContext context)
    {
        context.OperationDescription.Operation.Parameters.Add(
            new SwaggerParameter {
                Name = "Sample",
                Kind = SwaggerParameterKind.Header,
                Type …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core nswag

4
推荐指数
3
解决办法
8985
查看次数

标签 统计

asp.net-core ×1

c# ×1

nswag ×1