Web Api How to add a Header parameter for all API in Swagger

后端 未结 7 1095
温柔的废话
温柔的废话 2020-11-27 16:54

I searched for possible ways to add a request header parameter that would be added automatically to every method in my web-api but i couldn\'t find a clear one.

7条回答
  •  我在风中等你
    2020-11-27 17:11

    For Asp .Net MVC 5 you can use.
    Following the need to be done in Swagger Config file.

    private class AddAuthorizationHeaderParameter: IOperationFilter   // as a nested class in script config file.
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            if (operation.parameters == null)
                operation.parameters = new List();
    
            operation.parameters.Add(new Parameter
            {
                name = "Authorization",
                @in = "header",
                type = "string",
                required = true
            });
        }
    }
    
    c.OperationFilter(); // finally add this line in .EnableSwagger
    

    You can also add any no of headers for header implementation in Swagger.

提交回复
热议问题