gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:59:33 27 4
gpt4 key购买 nike

我需要添加自定义 header ,但无法弄清楚。我正在尝试使用新的 services.AddOpenApiDocument() 而不是 services.AddSwaggerDocument()。我想在整个 API 上添加这些自定义 header ,而不仅仅是单个方法或 Controller 。我尝试添加一个操作处理器,但是当我加载 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());
});

这是我的操作处理器:

public class SampleHeaderOperationProcessor : IOperationProcessor
{
public Task<bool> ProcessAsync(OperationProcessorContext context)
{
context.OperationDescription.Operation.Parameters.Add(
new SwaggerParameter {
Name = "Sample",
Kind = SwaggerParameterKind.Header,
Type = NJsonSchema.JsonObjectType.String,
IsRequired = false,
Description = "This is a test header",
Default = "{{\"field1\": \"value1\", \"field2\": \"value2\"}}"
});

return Task.FromResult(true);
}
}

我在配置()中唯一与此相关的东西:

    app.UseSwagger();
app.UseSwaggerUi3();

这是我的错误和控制台日志: My error and console log

如果有帮助,我正在使用 ASP .NET CORE 2.2NSwag.AspNetCore v12.1.0

最佳答案

这是我在项目中实现的一个示例。对于我来说,它工作正常:

enter image description here

接口(interface)“IOperationProcessor”的实现:

using NSwag;
using NSwag.SwaggerGeneration.Processors;
using NSwag.SwaggerGeneration.Processors.Contexts;
using System.Threading.Tasks;

namespace api.mstiDFE._Helpers.Swagger
{
public class AddRequiredHeaderParameter : IOperationProcessor
{
public Task<bool> ProcessAsync(OperationProcessorContext context)
{
context.OperationDescription.Operation.Parameters.Add(
new SwaggerParameter
{
Name = "token",
Kind = SwaggerParameterKind.Header,
Type = NJsonSchema.JsonObjectType.String,
IsRequired = false,
Description = "Chave de acesso à API, fornecida pela RevendaCliente",
Default = "Default Value"
});

return Task.FromResult(true);
}
}
}

startup.cs中的引用:

internal static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{

// Register the Swagger services
services.AddSwaggerDocument(config =>
{
// Adds the "token" parameter in the request header, to authorize access to the APIs
config.OperationProcessors.Add(new AddRequiredHeaderParameter());

config.PostProcess = document =>
{
document.Info.Version = "v1";
document.Info.Title = "Title ";
document.Info.Description = "API para geração de Documentos Fiscais Eletrônicos (DF-e) do projeto SPED";
document.Info.TermsOfService = "None";
document.Info.Contact = new NSwag.SwaggerContact
{
Name = "Name",
Email = "Email ",
Url = "Url "
};
document.Info.License = new NSwag.SwaggerLicense
{
Name = "Use under LICX",
Url = "https://example.com/license"
};

};
});
}

关于c# - 如何使用 C# .NET CORE 在 NSwag 文档中添加自定义 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55797528/

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