Swagger UI改造 增加 Token验证、显示控制器注释、自定义泛型缓存应用、
程序员文章站
2023-01-01 08:34:54
1 /// 2 /// Swagger 增加 Token 选项和控制器描述 3 /// 4 public class CustomOperationFilter : IOperationFilter 5 { 6 7 /// 8 /// 应 ......
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
个人追求:用最少且美观的代码,完成更多的秀。。。