1 /// <summary> 2 /// Swagger 增加 Token 选项和控制器描述 3 /// </summary> 4 public class CustomOperationFilter : IOperationFilter 5 { 6 7 /// <summary> 8 /// 应用 9 /// </summary> 10 /// <param name="operation">操作对象</param> 11 /// <param name="schemaRegistry">架构注册表对象</param> 12 /// <param name="apiDescription">API描述对象</param> 13 public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 14 { 15 if (operation.parameters == null) 16 { 17 operation.parameters = new List<Parameter>(); 18 } 19 var attribute = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>(); 20 if (attribute.Count == 0) 21 { 22 operation.parameters.Add(new Parameter { name = "Token", @in = "header", description = "令牌", required = false, type = "string" }); 23 } 24 string summary = CustomCache.GetCacheData(operation.tags[0], 25 () => 26 { 27 return GetControllerDescriptor(string.Format("T:{0}", apiDescription.ActionDescriptor.ControllerDescriptor.ControllerType.FullName)); 28 }, 0); 29 operation.tags[0] = summary; 30 } 31 32 /// <summary> 33 /// 获取控制器描述信息 34 /// </summary> 35 /// <param name="nodeName">节点名称</param> 36 /// <returns></returns> 37 private string GetControllerDescriptor(string nodeName) 38 { 39 XmlDocument xmlDocument = new XmlDocument(); 40 xmlDocument.Load(Constant.apiCommentDocuPath); 41 XmlNode node = xmlDocument.SelectSingleNode($"doc/members/member[@name='{nodeName}']"); 42 return node?.InnerText.Trim(); 43 } 44 }
自定义泛型缓存 转至 https://www.cnblogs.com/JingYeChong/p/10868473.html
需要获取完整Web API Demo 联系QQ 850856667 请遵守默认条件:1.转发请说明原作者 Clock@JY
个人追求:用最少且美观的代码,完成更多的秀。。。