Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

asp.net web api - Add individual custom headers in different controllers in web api using Swasbuckle

How can I add individual headers on different controllers. E.g.:

Controller Name: Controller1, Custom header: Header1

Controller Name: Controller2, Custom header: Header2

The headers should be displayed for all the apis under the specific controller

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This can be solved by adding an OperationFilter to your swagger configuration. First you have to provide a class that implements IOperationFilter. The Applymethod receives an Operation parameter which contains the controller name in the tagfield. When the Swagger UI is rendered, the Applymethod will be called for each method in the API. You could even provide individual parameters for each API method, as Operation also contains the operationId.

public class AddRequiredHeaderParameter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.parameters == null)
            operation.parameters = new List<Parameter>();

        if (operation.tags[0]?.CompareTo("Example") == 0)
        {
            operation.parameters.Add(new Parameter
            {
                name = "X-ExampleParam",
                @in = "header",
                @default = "42",  // optional default value, can be omitted
                type = "string",
                description = "My special parameter for the example API",
                required = true
            });
        }
        else if (operation.tags[0]?.CompareTo("Whatever") == 0)
        {
        // add other header parameters here
        }
    }
} 

In the debugger, with a controller named ExampleController, it looks like this:

debug output

The result in the Swagger UI is a special parameter that is only applied to the API of my Example controller: swagger ui showing special parameter

Tell Swagger to use your OperationFilter by adding one line in the Register method of the SwaggerConfig class:

public class SwaggerConfig
{
    public static void Register(HttpConfiguration config)
    {
        var thisAssembly = typeof(SwaggerConfig).Assembly;

        //GlobalConfiguration.Configuration
        config
            .EnableSwagger(c =>
                {
                ... // omitted some lines here
                c.OperationFilter<AddRequiredHeaderParameter>(); // Add this line
                ... // omitted some lines here
                })

     } 

The idea to this solution is based on ShaTin's answer: How to send custom headers with requests in Swagger UI?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...