asp.net webapi+swagger+OAuth2.0
程序员文章站
2024-03-31 08:04:46
文档继续完善整理中。。。。。。 c.DocumentFilter(); /// /// Swagger注释帮助类 /// public class SwaggerDocTag : IDocumentFilter { ///
swagger显示控制器注释
文档继续完善整理中。。。。。。
c.documentfilter<swaggerdoctag>(); /// <summary> /// swagger注释帮助类 /// </summary> public class swaggerdoctag : idocumentfilter { /// <summary> /// 添加附加注释 /// </summary> public void apply(swaggerdocument swaggerdoc, schemaregistry schemaregistry, iapiexplorer apiexplorer) { //swaggerdoc.tags = new list<tag> //{ // //添加对应的控制器描述 这个是我好不容易在issues里面翻到的 // new tag { name = "home", description = "后台" }, // new tag { name = "client", description = "客户端" }, // new tag { name = "system", description = "系统" } //}; swaggerdoc.tags = getcontrollerdesc(); } /// <summary> /// 从xml注释中读取控制器注释 /// </summary> /// <returns></returns> private list<tag> getcontrollerdesc() { list<tag> taglist = new list<tag>(); var xmlpath = string.format(string.format("{0}/bin/eternal.webapi.xml", system.appdomain.currentdomain.basedirectory)); if (!file.exists(xmlpath))//检查xml注释文件是否存在 return taglist; xmldocument xmldoc = new xmldocument(); xmldoc.load(xmlpath); string membername = string.empty;//xml三级节点的name属性值 string controllername = string.empty;//控制器完整名称 string key = string.empty;//控制器去controller名称 string value = string.empty;//控制器注释 foreach (xmlnode node in xmldoc.selectnodes("//member"))//循环三级节点member { membername = node.attributes["name"].value; if (membername.startswith("t:"))//t:开头的代表类 { string[] arrpath = membername.split('.'); controllername = arrpath[arrpath.length - 1]; if (controllername.endswith("controller"))//controller结尾的代表控制器 { xmlnode summarynode = node.selectsinglenode("summary");//注释节点 key = controllername.remove(controllername.length - "controller".length, "controller".length); if (summarynode != null && !string.isnullorempty(summarynode.innertext) && !taglist.contains(new tag { name = key })) { value = summarynode.innertext.trim(); taglist.add(new tag { name = key, description = value }); } } } } return taglist; }