zoukankan      html  css  js  c++  java
  • .net core Swagger 过滤部分Api

    因为场景需要,要把某些特定的api过滤掉,不允许显示在swaggerui里,

    具体操作步骤: 分为三步

    步骤1: 创建Attribute     

    1     /// <summary>
    2     /// ignore some api on swagger.json
    3     /// </summary>
    4     [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    5     public class SwaggerIgnoreAttribute : Attribute
    6     {
    7 
    8     }

    步骤2:创建IDocumentFilter的实现类XXXFileter

     /// <summary>
        /// 过滤具备SwaggerIgnore特性的api
        /// </summary>
        public class SwaggerIgnoreFilter : IDocumentFilter
        {
    
            public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
            {
               
                var ignoreApis = context.ApiDescriptions.Where(wh => wh.ActionAttributes().Any(any => any is SwaggerIgnoreAttribute));
                if (ignoreApis != null)
                {
                    foreach (var ignoreApi in ignoreApis)
                    {
                        swaggerDoc.Paths.Remove("/" + ignoreApi.RelativePath);
                    }
                }
    
            }
    
        }
    View Code

    步骤3:StartUp类中 Swagger的Config中使用过滤器

     services.AddSwaggerGen(
                    options =>
                    {
                        options.DocumentFilter<SwaggerIgnoreFilter>();
                    });
  • 相关阅读:
    数组splice用法
    opacity 适配Ie
    直接贴页面,页面衔接处总会有一像素的间隔
    <script src='url'</script>显示问题
    弹出层
    CF789A. Anastasia and pebbles
    CF789C. Functions again
    HDU2161 Primes
    UVA11752 The Super Powers
    UVA11827 Maximum GCD
  • 原文地址:https://www.cnblogs.com/mailaidedt/p/10132665.html
Copyright © 2011-2022 走看看