/// <summary> /// Swagger 增加 Token 选项和控制器描述 /// </summary> public class CustomOperationFilter : IOperationFilter { /// <summary> /// 应用 /// </summary> /// <param name="operation">操作对象</param> /// <param name="schemaRegistry">架构注册表对象</param> /// <param name="apiDescription">API描述对象</param> public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { if (operation.parameters == null) { operation.parameters = new List<Parameter>(); } if (!apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any()) { operation.parameters.Add(new Parameter { name = "Token", @in = "header", description = "令牌", required = false, type = "string" }); } ], () => { return GetControllerDescriptor(string.Format("T:{0}", apiDescription.ActionDescriptor.ControllerDescriptor.ControllerType.FullName)); }, ); operation.tags[] = summary; } /// <summary> /// 获取控制器描述信息 /// </summary> /// <param name="nodeName">节点名称</param> /// <returns></returns> private string GetControllerDescriptor(string nodeName) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(Constant.apiCommentDocuPath); XmlNode node = xmlDocument.SelectSingleNode($"doc/members/member[@name='{nodeName}']"); return node?.InnerText.Trim(); } }
查看核心代码
自定义泛型缓存 转至 https://www.cnblogs.com/JingYeChong/p/10868473.html
需要获取完整Web API Demo 联系QQ 850856667
个人追求:用最少且美观的代码,完成更多的秀。。。