C# Swashbuckle仅向某些方法添加自定义标头

C# Swashbuckle仅向某些方法添加自定义标头,c#,asp.net-core,swagger,C#,Asp.net Core,Swagger,我已经在.net核心应用程序中使用IOperationFilter成功地添加了自定义头,现在的问题是如何仅在SomeController类中过滤某些方法的头。这是可以实现的吗?这是我当前的代码: Startup.cs public void配置服务(IServiceCollection服务) { services.AddSwaggerGen(c=> { //这里有些招摇过市的服务。 c、 操作过滤器(); }); } SomeFilter.cs public类SomeFilter:IOpera

我已经在.net核心应用程序中使用
IOperationFilter
成功地添加了自定义头,现在的问题是如何仅在
SomeController
类中过滤某些方法的头。这是可以实现的吗?这是我当前的代码:

Startup.cs

public void配置服务(IServiceCollection服务)
{
services.AddSwaggerGen(c=>
{
//这里有些招摇过市的服务。
c、 操作过滤器();
});
}
SomeFilter.cs

public类SomeFilter:IOperationFilter
{
公共无效应用(OpenApiOperation操作,OperationFilterContext上下文)
{
if(operation.Parameters==null)
operation.Parameters=newlist();
operation.Parameters.Add(新的OpenApiParameter
{
Name=“某些自定义标题”,
In=参数位置.Header,
必需=错误,
Schema=newopenapischema
{
Type=“String”
}
});
}
}
SomeController.cs

[ApiController]
[路线(“[控制器]”)]
公共类控制器:控制器
{
[异名]
[HttpPost(“某些方法1”)]
public IAction SomeMethod1()//此方法不应包括自定义标头筛选器
{
返回Ok();
}
[授权]
[HttpPost(“具有授权的某些方法2”)]
public IAction SomeMethod2()//这应该具有swagger中的自定义头
{
返回Ok();
}
[异名]
[HttpGet(“未经授权的某些方法”)]
public IAction SomeMethod3()//这应该具有swagger中的自定义头
{
返回Ok();
}
}

我找到了一种排除方法的方法,通过这样做可以排除上述方法:

public类SomeFilter:IOperationFilter
{
公共无效应用(OpenApiOperation操作,OperationFilterContext上下文)
{
var methodName=context.apiscription.ActionDescriptor.As().ActionName;
var hasExcludedMethod=ApiFilterSettings.AppSettingsFilterMethods.ToString().Contains(methodName);
如果(已排除方法)
return;//找到排除的方法时直接返回;**强文本**
if(operation.Parameters==null)
operation.Parameters=newlist();
operation.Parameters.Add(新的OpenApiParameter
{
Name=“某些自定义标题”,
In=参数位置.Header,
必需=错误,
Schema=newopenapischema
{
Type=“String”
}
});
}
}
我希望这能帮助你们