0

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"
            }
        );
    }
);

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Not really, just their GitHub code is closest, cheers mate!: github.com/exyi/NSwag/blob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.