1、Nuget安装Swashbuckle.AspNetCore
2、新建HiddenApiFilter类,
/// <summary>
/// 隐藏不需要显示的接口
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public partial class HiddenApiAttribute : Attribute { }
public class HiddenApiFilter : IDocumentFilter
{
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;
List<string> list = new List<string>();
//添加需要隐藏控制器
list.Add("Access");
list.Add("admin");
list.Add("AirConditioner");
list.Add("Alarm");
list.Add("ArchiverDetails");
list.Add("ArchivesBox");
list.Add("ArchivesTypeClass");
list.Add("Area");
list.Add("Audit");
list.Add("DenseFrameTask");
list.Add("device");
list.Add("EnvironmentalEquipment");
list.Add("Equipment");
list.Add("Gygzz");
list.Add("device");
list.Add("iclock");
list.Add("Irealer");
list.Add("Login");
list.Add("MaintenanceInstructions");
list.Add("MonitoringSystemSettings");
list.Add("PDA");
list.Add("Permission");
list.Add("ReportStatisticsService");
list.Add("RFIDInventory");
list.Add("RFIDTask");
list.Add("Role");
list.Add("ScanCode");
list.Add("SystemSettings");
list.Add("task");
list.Add("ThreeDimensional");
list.Add("Threshold");
list.Add("tools");
list.Add("User");
list.Add("Video");
list.Add("WareHouse");
list.Add("Archives");
if (list.Contains(api.ControllerName))//过滤的核心逻辑
{
string key = "/" + apiDescription.RelativePath;
if (key.Contains("?"))
{
int idx = key.IndexOf("?", StringComparison.Ordinal);
key = key.Substring(0, idx);
}
swaggerDoc.Paths.Remove(key);
}
}
}
}
3、在Startup.cs类中的AddSwaggerGen方法中添加 c.DocumentFilter<HiddenApiFilter>();
c.DocumentFilter<HiddenApiFilter>();