项目梳理4——WebApi
Posted 晴天彩虹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目梳理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中,,Register(HttpConfiguration config)方法的开头,添加代码:
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 = 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的主要内容,如果未能解决你的问题,请参考以下文章
java 注册过程 - android片段,Asp.net WebApi