Search code examples
c#asp.net-web-apiswaggernswag

Web API add a Header parameter for all Endpoints in Swagger using NSwag+Owin


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"

            }
        );
    }
);

Solution

  • You 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.