Swagger 如何向API.NET web API的特定控制器/方法添加头参数

Swagger 如何向API.NET web API的特定控制器/方法添加头参数,swagger,swagger-ui,Swagger,Swagger Ui,我知道我可以为swagger中的所有API添加头参数,如中所述 使用这种方法,header参数将显示在所有API的swagger UI中 但并不是我所有的API/控制器都需要header参数,有没有办法只为特定的控制器甚至是特定的API添加header?虽然这篇文章很旧,但我认为如果新来者陷入困境,它会对他们有所帮助 在同样的情况下 public class AddRequiredHeaderParameter : IOperationFilter { public void Apply

我知道我可以为swagger中的所有API添加头参数,如中所述

使用这种方法,header参数将显示在所有API的swagger UI中


但并不是我所有的API/控制器都需要header参数,有没有办法只为特定的控制器甚至是特定的API添加header?

虽然这篇文章很旧,但我认为如果新来者陷入困境,它会对他们有所帮助 在同样的情况下

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

       /*System.Diagnostics.Trace.WriteLine(apiDescription.RelativePath +
          "=paath");*/
        if (apiDescription.RelativePath.Contains(***{url***}))
        {
            operation.parameters.Add(new Parameter
            {
                name = "X-User-Token",
                @in = "header",
                type = "string",
                required = false,
                description=""
            });
            operation.parameters.Add(new Parameter
            {
                name = "authorization",
                @in = "header",
                description = "Token",
                type = "string",
                required = true
            });
        }
        else
        {
            operation.parameters.Add(new Parameter
            {
                name = "X-User-Token",
                @in = "header",
                type = "string",
                required = false,
                description="description"
            });
        }
    }
}
公共类addRequiredHeader参数:IOperationFilter
{
公共无效应用(操作、方案注册、方案注册、,
ApiDescription(ApiDescription)
{
if(operation.parameters==null)
{
operation.parameters=新列表();
}
/*系统.诊断.跟踪.写入线(apiDescription.RelativePath+
“=paath”)*/
if(apiscription.RelativePath.Contains(****{url***}))
{
操作.参数.添加(新参数
{
name=“X-User-Token”,
@in=“header”,
type=“string”,
必需=错误,
description=“”
});
操作.参数.添加(新参数
{
name=“授权”,
@in=“header”,
description=“令牌”,
type=“string”,
必需=真
});
}
其他的
{
操作.参数.添加(新参数
{
name=“X-User-Token”,
@in=“header”,
type=“string”,
必需=错误,
description=“description”
});
}
}
}

虽然这篇文章很旧,但我认为如果新来者陷入困境,它会对他们有所帮助 在同样的情况下

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

       /*System.Diagnostics.Trace.WriteLine(apiDescription.RelativePath +
          "=paath");*/
        if (apiDescription.RelativePath.Contains(***{url***}))
        {
            operation.parameters.Add(new Parameter
            {
                name = "X-User-Token",
                @in = "header",
                type = "string",
                required = false,
                description=""
            });
            operation.parameters.Add(new Parameter
            {
                name = "authorization",
                @in = "header",
                description = "Token",
                type = "string",
                required = true
            });
        }
        else
        {
            operation.parameters.Add(new Parameter
            {
                name = "X-User-Token",
                @in = "header",
                type = "string",
                required = false,
                description="description"
            });
        }
    }
}
公共类addRequiredHeader参数:IOperationFilter
{
公共无效应用(操作、方案注册、方案注册、,
ApiDescription(ApiDescription)
{
if(operation.parameters==null)
{
operation.parameters=新列表();
}
/*系统.诊断.跟踪.写入线(apiDescription.RelativePath+
“=paath”)*/
if(apiscription.RelativePath.Contains(****{url***}))
{
操作.参数.添加(新参数
{
name=“X-User-Token”,
@in=“header”,
type=“string”,
必需=错误,
description=“”
});
操作.参数.添加(新参数
{
name=“授权”,
@in=“header”,
description=“令牌”,
type=“string”,
必需=真
});
}
其他的
{
操作.参数.添加(新参数
{
name=“X-User-Token”,
@in=“header”,
type=“string”,
必需=错误,
description=“description”
});
}
}
}