新建一个特性类SwaggerIgnoreAttribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class SwaggerIgnoreAttribute : Attribute { }
新建一个SwaggerIgnoreFilter 继承 IDocumentFilter实现Apply方法重写实现过滤
public class SwaggerIgnoreFilter : IDocumentFilter { public string[] hiddeApi = new string[] { "Quartz" }; /// <summary> /// 重写Apply方法,移除隐藏接口的生成 /// </summary> /// <param name="swaggerDoc">swagger文档文件</param> /// <param name="context"></param> public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { foreach (ApiDescription apiDescription in context.ApiDescriptions) { var api = apiDescription.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor; //这里强转来获取到控制器的名称 var contoller = api.ControllerName; if (api.ControllerName.IsContainsIn(hiddeApi))//过滤的核心逻辑 { string key = "/" + apiDescription.RelativePath; if (key.Contains("?")) { int idx = key.IndexOf("?", StringComparison.Ordinal); key = key.Substring(0, idx); } swaggerDoc.Paths.Remove(key); } } } }
然后在Startup.cs文件中的swagger 配置中使用
评论区