• 项目梳理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;
            }
        }
    

      

      

  • 相关阅读:
    Swagger2 添加HTTP head参数
    获取枚举类型描述
    JS设置cookie、读取cookie、删除cookie
    ES6中Promise的入门(结合例子)
    阮一峰的ES6---Promise对象
    model_util.py
    IfcSpatialElementType
    labelme coco
    python opencv KeyPoint
    IfcSpatialZoneType
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/7300863.html
Copyright © 2020-2023  润新知