zoukankan      html  css  js  c++  java
  • 项目梳理4——WebApi项目,使用注释填充Description字段

    web.config中添加连接字符串:

    为webapi添加Description,使用注释来填充此字段

    对于所有引用的xxxx.base项目设置生成的xml文档,注意release、debug下都需设置,并重新生成

    xxxxx.WebApiAreasHelpPageXmlDocumentationProvider.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.WebApiAreasHelpPageApp_StartHelpPageConfig.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.WebApiAreasHelpPage路径下添加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;
            }
        }
    

      

      

  • 相关阅读:
    STL(七)之萃取技术
    STL(六)之空间配置器
    为Oracle配置监听
    Oracle11.2.01安装过程
    SVN简介
    SVN客户端安装教程
    SVN服务器安装教程
    排序算法-冒泡排序
    使用Struts2实现超级文本的链接
    排序算法-快速排序
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/7300863.html
Copyright © 2011-2022 走看看