简体   繁体   English

Swashbuckle 仅在某些方法中添加自定义 header

[英]Swashbuckle add custom header to some methods only

I have successfully add custom header using IOperationFilter in my .net core application, now my problem is how to filter it out for certain methods only in the SomeController class.我已经在我的 .net 核心应用程序中使用IOperationFilter成功添加了自定义 header,现在我的问题是如何仅在SomeController class 中针对某些方法将其过滤掉。 Is this achievable?这是可以实现的吗? This is my current code:这是我当前的代码:

Startup.cs启动.cs

public void ConfigureServices(IServiceCollection services)
{
    
    services.AddSwaggerGen(c =>
    {
        // some swagger services here.
        c.OperationFilter<SomeFilter>();
    });
}

SomeFilter.cs一些过滤器.cs

public class SomeFilter: IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

        operation.Parameters.Add(new OpenApiParameter
        {
            Name = "Some-Custom-Header",
            In = ParameterLocation.Header,
            Required = false,
            Schema = new OpenApiSchema
            {
                Type = "String"
            }
        });
    }

}

SomeController.cs SomeController.cs

[ApiController]
[Route("[controller]")]
public class SomeController: Controller
{
    [AllowAnonymous]
    [HttpPost("some_method1")]
    public IAction SomeMethod1() // this method should not include custom header filter
    {
        return Ok();
    }
    
    [Authorize]
    [HttpPost("some_method2_with_authorize")]
    public IAction SomeMethod2() // this should have the custom header in swagger
    {
        return Ok();
    }

    [AllowAnonymous]
    [HttpGet("some_method3_without_authorize")]
    public IAction SomeMethod3() // this should have the custom header in swagger
    {
        return Ok();
    }

}

I found a way on how to exclude the method that will exclude the said methods by doing this:我找到了一种方法来排除将排除上述方法的方法:

public class SomeFilter: IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        var methodName = context.ApiDescription.ActionDescriptor.As<ControllerActionDescriptor>().ActionName;
        var hasExcludedMethod = ApiFilterSettings.AppSettingsFilterMethods.ToStringList().Contains(methodName);
        if(hasExcludedMethod)
           return; // return directly when an excluded method is found;**strong text**

        if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

        operation.Parameters.Add(new OpenApiParameter
        {
            Name = "Some-Custom-Header",
            In = ParameterLocation.Header,
            Required = false,
            Schema = new OpenApiSchema
            {
                Type = "String"
            }
        });
    }

}

I hope this will help you guys.我希望这会对你们有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM