.net core 3.1 Swagger 过滤部分Api
因为场景需要,要把某些特定的api过滤掉,不允许显示在swaggerui里,
具体操作步骤: 分为三步
步骤1: 创建Attribute
步骤2:创建IDocumentFilter的实现类XXXFileter
/// <summary> /// 隐藏接口,不生成到swagger文档展示 /// </summary> [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public partial class SwaggerIgnoreAttribute : Attribute { } public class SwaggerIgnoreFilter : IDocumentFilter { public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { var ignoreApis = context.ApiDescriptions.Where(wh => wh.ActionDescriptor.EndpointMetadata.Any(any => any is SwaggerIgnoreAttribute)); if (ignoreApis != null) { foreach (var ignoreApi in ignoreApis) { swaggerDoc.Paths.Remove("/" + ignoreApi.RelativePath); } } } }
步骤3:StartUp类中 Swagger的Config中使用过滤器
services.AddSwaggerGen(c => { c.DocumentFilter<SwaggerIgnoreFilter>(); });
原地址:https://www.cnblogs.com/mailaidedt/p/10132665.html