我需要添加自定义标头,但无法弄清楚。我正在尝试使用新的 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());
});
这是我的操作处理器:
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);
}
}
我在 Configure() 中唯一与此有关的东西:
app.UseSwagger();
app.UseSwaggerUi3();
这是我的错误和控制台日志: 我的错误和控制台日志
如果它有帮助,我正在使用ASP .NET CORE 2.2和NSwag.AspNetCore v12.1.0
海绵宝宝撒
波斯汪
小唯快跑啊
相关分类