项目梳理4——WebApi项目,使用注释填充Description字段
web.config中添加连接字符串:
为webapi添加Description,使用注释来填充此字段
对于所有引用的xxxx.base项目设置生成的xml文档,注意release、debug下都需设置,并重新生成
xxxxx.WebApi\Areas\HelpPage\XmlDocumentationProvider.cs中,添加构造函数,用于解析xml文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 | /// <summary> /// Initializes a new instance of the <see cref="XmlDocumentationProvider"/> class. /// </summary> /// <param name="documentPath">The physical path to XML document.</param> public XmlDocumentationProvider( string documentPath) { if (documentPath == null ) { throw new ArgumentNullException( "documentPath" ); } XPathDocument xpath = new XPathDocument(documentPath); _documentNavigator = xpath.CreateNavigator(); } |
xxxxx.WebApi\Areas\HelpPage\App_Start\HelpPageConfig.cs中,,Register(HttpConfiguration config)方法的开头,添加代码:
1 2 3 4 5 6 7 8 9 10 11 12 | var server = HttpContext.Current.Server; String[] xmlFiles = System.IO.Directory.GetFiles(server.MapPath( "~/App_Data" ), "*.xml" ); if (xmlFiles == null || xmlFiles.Length == 0) { xmlFiles = System.IO.Directory.GetFiles(server.MapPath( "~/Bin" ), "*.xml" ); } if (xmlFiles != null && xmlFiles.Length > 0) { MultipleXmlDocumentationProvider mp = new MultipleXmlDocumentationProvider(xmlFiles); config.SetDocumentationProvider(mp); } |
xxxxx.WebApi\Areas\HelpPage路径下添加MultipleXmlDocumentationProvider.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | public class MultipleXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider { List<XmlDocumentationProvider> _listProviders = new List<XmlDocumentationProvider>(); public MultipleXmlDocumentationProvider( params String[] xmlDocPaths) { InitDocuments(xmlDocPaths); } /// <summary> /// 初始化文档,主要修复SeeAlso部分内容 /// </summary> /// <param name="xmlDocPaths">xml文档路径</param> protected void InitDocuments( params String[] xmlDocPaths) { var listXmlDocs = new List<XDocument>(); foreach ( var xmlPath in xmlDocPaths) { var content = System.IO.File.ReadAllText(xmlPath); listXmlDocs.Add(XDocument.Parse(content)); } var dictNameNodes = new Dictionary<String, XElement>(); foreach ( var xmlDoc in listXmlDocs) { var methodNodes = xmlDoc.Root.Descendants().Where(n => n.Name.LocalName == "member" ); foreach ( var mn in methodNodes) { var name = mn.Attribute( "name" ).Value; dictNameNodes[name] = mn; } } foreach ( var xmlDoc in listXmlDocs) { var saNodes = xmlDoc.Root.Descendants().Where(n => n.Name.LocalName == "seealso" ).ToList(); foreach ( var saNode in saNodes) { if (saNode.Attribute( "cref" ) == null ) continue ; var crefValue = saNode.Attribute( "cref" ).Value; if (dictNameNodes.ContainsKey(crefValue)) { var a = dictNameNodes[crefValue]; XLinqUtil.ReplaceOuterXml(saNode, XLinqUtil.InnerXML(a)); } //XLinqUtil.ReplaceInnerXml(saNode,) } } foreach ( var xmlDoc in listXmlDocs) { using ( var ms = new System.IO.StringReader(xmlDoc.ToString(SaveOptions.OmitDuplicateNamespaces))) { _listProviders.Add( new XmlDocumentationProvider(ms)); } } } public string GetDocumentation(System.Web.Http.Controllers.HttpParameterDescriptor parameterDescriptor) { String result = null ; foreach ( var provider in _listProviders) { result = provider.GetDocumentation(parameterDescriptor); if (result != null ) return result; } return result; } public string GetDocumentation(System.Web.Http.Controllers.HttpActionDescriptor actionDescriptor) { String result = null ; foreach ( var provider in _listProviders) { result = provider.GetDocumentation(actionDescriptor); if (result != null ) return result; } return result; } public string GetDocumentation(System.Web.Http.Controllers.HttpControllerDescriptor controllerDescriptor) { String result = null ; foreach ( var provider in _listProviders) { result = provider.GetDocumentation(controllerDescriptor); if (result != null ) return result; } return result; } public string GetResponseDocumentation(System.Web.Http.Controllers.HttpActionDescriptor actionDescriptor) { String result = null ; foreach ( var provider in _listProviders) { result = provider.GetResponseDocumentation(actionDescriptor); if (result != null ) return result; } return result; } public string GetDocumentation(System.Reflection.MemberInfo member) { String result = null ; foreach ( var provider in _listProviders) { result = provider.GetDocumentation(member); if (result != null ) return result; } return result; } public string GetDocumentation(Type type) { String result = null ; foreach ( var provider in _listProviders) { result = provider.GetDocumentation(type); if (result != null ) return result; } return result; } } |
分类:
实战
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?