首页 > 代码库 > 项目梳理4——WebApi
项目梳理4——WebApi
web.config中添加连接字符串:
为webapi添加Description,使用注释来填充此字段
对于所有引用的xxxx.base项目设置生成的xml文档,注意release、debug下都需设置,并重新生成
xxxxx.WebApi\Areas\HelpPage\XmlDocumentationProvider.cs中添加xml文件的解析代码:
/// <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添加代码:
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
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 = http://www.mamicode.com/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; } }
项目梳理4——WebApi
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。