admin管理员组文章数量:1123699
I wish to add a custom header parameter in Swagger for all my endpoints in my ASP.NET Web API application running ASP.NET 4.8 using NSwag.
I have found many, many references explaining how to do this in .NET Core (.NET Core popular example), but very few in .NET Framework, and the references I have found for .NET Framework (.NET Framework example) don't match how my application is configured.
Is there a way to add a custom header when my application is configured as follows? (This is the configuration I got when I followed the package instructions which recommended the Owin integration).
RouteTable.Routes.MapOwinPath(
"swagger",
app =>
{
app.UseSwaggerUi(
typeof(WebApiApplication).Assembly,
settings =>
{
settings.MiddlewareBasePath = "/swagger";
settings.GeneratorSettings.DefaultUrlTemplate =
"api/{controller}/{action}/{id}";
settings.DocumentTitle = "My API";
settings.GeneratorSettings.Title = "My API";
settings.GeneratorSettings.Description = "My API"
}
);
}
);
I wish to add a custom header parameter in Swagger for all my endpoints in my ASP.NET Web API application running ASP.NET 4.8 using NSwag.
I have found many, many references explaining how to do this in .NET Core (.NET Core popular example), but very few in .NET Framework, and the references I have found for .NET Framework (.NET Framework example) don't match how my application is configured.
Is there a way to add a custom header when my application is configured as follows? (This is the configuration I got when I followed the package instructions which recommended the Owin integration).
RouteTable.Routes.MapOwinPath(
"swagger",
app =>
{
app.UseSwaggerUi(
typeof(WebApiApplication).Assembly,
settings =>
{
settings.MiddlewareBasePath = "/swagger";
settings.GeneratorSettings.DefaultUrlTemplate =
"api/{controller}/{action}/{id}";
settings.DocumentTitle = "My API";
settings.GeneratorSettings.Title = "My API";
settings.GeneratorSettings.Description = "My API"
}
);
}
);
Share
Improve this question
edited 13 hours ago
Dale K
asked yesterday
Dale KDale K
27.1k15 gold badges55 silver badges82 bronze badges
1 Answer
Reset to default 2You can add a custom header parameter for all endpoints by configuring the Swagger generator settings.
Add an Operation Filter
using System.Web.Http.Description;
using NSwag.Generation.Processors;
using NSwag.Generation.Processors.Contexts;
public class AddCustomHeaderOperationFilter : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
{
// Add a custom header parameter
context.OperationDescription.Operation.Parameters.Add(new NSwag.OpenApiParameter
{
Name = "X-Custom-Header",
Kind = NSwag.OpenApiParameterKind.Header,
Type = NJsonSchema.JsonObjectType.String,
Description = "Custom header for all endpoints",
IsRequired = false
});
return true;
}
}
Register the Filter in Your Configuration
Modify your OWIN Swagger configuration to include the operation filter:
RouteTable.Routes.MapOwinPath(
"swagger",
app =>
{
app.UseSwaggerUi(
typeof(WebApiApplication).Assembly,
settings =>
{
settings.MiddlewareBasePath = "/swagger";
settings.GeneratorSettings.DefaultUrlTemplate =
"api/{controller}/{action}/{id}";
settings.DocumentTitle = "My API";
settings.GeneratorSettings.Title = "My API";
settings.GeneratorSettings.Description = "My API";
// Add the custom header filter
settings.GeneratorSettings.OperationProcessors.Add(new AddCustomHeaderOperationFilter());
}
);
}
);
Note: You can also make the header required by setting IsRequired = true
in the AddCustomHeaderOperationFilter
implementation.
Note: This is for NSWag implementation. For Swagger, it is a little different. You would implement IOperationFilter
(Swashbuckle's version) instead.
本文标签: cWeb API add a Header parameter for all Endpoints in Swagger using NSwagOwinStack Overflow
版权声明:本文标题:c# - Web API add a Header parameter for all Endpoints in Swagger using NSwag+Owin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736591085a1945078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论