WebApi升级到2.0以后的XmlDocumentationProvider

using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Web.Http.Controllers;
using System.Web.Http.Description;
using System.Xml.XPath;
using Tiza.CStar.ErpApi.Areas.HelpPage.ModelDescriptions;

namespace Tiza.CStar.ErpApi.Areas.HelpPage
{
    /// <summary>
    /// A custom <see cref="IDocumentationProvider"/> that reads the API documentation from an XML documentation file.
    /// </summary>
    public class XmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
    {
        private XPathNavigator _documentNavigator;
        private const string TypeExpression = "/doc/members/member[@name=‘T:{0}‘]";
        private const string MethodExpression = "/doc/members/member[@name=‘M:{0}‘]";
        private const string PropertyExpression = "/doc/members/member[@name=‘P:{0}‘]";
        private const string FieldExpression = "/doc/members/member[@name=‘F:{0}‘]";
        private const string ParameterExpression = "param[@name=‘{0}‘]";

        /// <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();
        }

        public string GetDocumentation(HttpControllerDescriptor controllerDescriptor)
        {
            XPathNavigator typeNode = GetTypeNode(controllerDescriptor.ControllerType);
            return GetTagValue(typeNode, "summary");
        }

        public virtual string GetDocumentation(HttpActionDescriptor actionDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(actionDescriptor);
            return GetTagValue(methodNode, "summary");
        }

        public virtual string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
        {
            ReflectedHttpParameterDescriptor reflectedParameterDescriptor = parameterDescriptor as ReflectedHttpParameterDescriptor;
            if (reflectedParameterDescriptor != null)
            {
                XPathNavigator methodNode = GetMethodNode(reflectedParameterDescriptor.ActionDescriptor);
                if (methodNode != null)
                {
                    string parameterName = reflectedParameterDescriptor.ParameterInfo.Name;
                    XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, ParameterExpression, parameterName));
                    if (parameterNode != null)
                    {
                        return parameterNode.Value.Trim();
                    }
                }
            }

            return null;
        }

        public string GetResponseDocumentation(HttpActionDescriptor actionDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(actionDescriptor);
            return GetTagValue(methodNode, "returns");
        }

        public string GetDocumentation(MemberInfo member)
        {
            string memberName = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", GetTypeName(member.DeclaringType), member.Name);
            string expression = member.MemberType == MemberTypes.Field ? FieldExpression : PropertyExpression;
            string selectExpression = String.Format(CultureInfo.InvariantCulture, expression, memberName);
            XPathNavigator propertyNode = _documentNavigator.SelectSingleNode(selectExpression);
            return GetTagValue(propertyNode, "summary");
        }

        public string GetDocumentation(Type type)
        {
            XPathNavigator typeNode = GetTypeNode(type);
            return GetTagValue(typeNode, "summary");
        }

        private XPathNavigator GetMethodNode(HttpActionDescriptor actionDescriptor)
        {
            ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor;
            if (reflectedActionDescriptor != null)
            {
                string selectExpression = String.Format(CultureInfo.InvariantCulture, MethodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo));
                return _documentNavigator.SelectSingleNode(selectExpression);
            }

            return null;
        }

        private static string GetMemberName(MethodInfo method)
        {
            string name = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", GetTypeName(method.DeclaringType), method.Name);
            ParameterInfo[] parameters = method.GetParameters();
            if (parameters.Length != 0)
            {
                string[] parameterTypeNames = parameters.Select(param => GetTypeName(param.ParameterType)).ToArray();
                name += String.Format(CultureInfo.InvariantCulture, "({0})", String.Join(",", parameterTypeNames));
            }

            return name;
        }

        private static string GetTagValue(XPathNavigator parentNode, string tagName)
        {
            if (parentNode != null)
            {
                XPathNavigator node = parentNode.SelectSingleNode(tagName);
                if (node != null)
                {
                    return node.Value.Trim();
                }
            }

            return null;
        }

        private XPathNavigator GetTypeNode(Type type)
        {
            string controllerTypeName = GetTypeName(type);
            string selectExpression = String.Format(CultureInfo.InvariantCulture, TypeExpression, controllerTypeName);
            return _documentNavigator.SelectSingleNode(selectExpression);
        }

        private static string GetTypeName(Type type)
        {
            string name = type.FullName;
            if (type.IsGenericType)
            {
                // Format the generic type name to something like: Generic{System.Int32,System.String}
                Type genericType = type.GetGenericTypeDefinition();
                Type[] genericArguments = type.GetGenericArguments();
                string genericTypeName = genericType.FullName;

                // Trim the generic parameter counts from the name
                genericTypeName = genericTypeName.Substring(0, genericTypeName.IndexOf(‘`‘));
                string[] argumentTypeNames = genericArguments.Select(t => GetTypeName(t)).ToArray();
                name = String.Format(CultureInfo.InvariantCulture, "{0}{{{1}}}", genericTypeName, String.Join(",", argumentTypeNames));
            }
            if (type.IsNested)
            {
                // Changing the nested type name from OuterType+InnerType to OuterType.InnerType to match the XML documentation syntax.
                name = name.Replace("+", ".");
            }

            return name;
        }
    }
}
时间: 2025-01-07 19:19:16

WebApi升级到2.0以后的XmlDocumentationProvider的相关文章

zabbix2.2升级到3.0.4

需求:zabbix2.2升级到3.0.4 备注:zabbix 2.5之后对php有严格要求,php版本必须大于php5.4 php升级可以覆盖安装,也可以多处安装再选择,此处选择后者. 安装php5.6.24 yum install -y libxml2 libxml2-devel \ openssl openssl-devel \ curl* libcurl* \ libvpx* libjpeg* \ libpng libpng-devel \ freetype* t1lib* \ zlib

Ubuntu 16.04升级4.7.0内核后导致Compiz奔溃,问题:compiz[4852]: segfault at 48 ip 00007f88cae087f0 sp 00007ffce354c268 error 4 in libscale.so

由于硬件的驱动支持问题,升级4.7.0的内核主要是为了能使用Intel HD Graphics 630驱动,但是也出现了相关问题,比如Compiz的特效导致桌面上如果有多个相同程序启动,然后再次点击时就会出现闪烁和崩溃问题.查找日志如下: compiz[4852]: segfault at 48 ip 00007f88cae087f0 sp 00007ffce354c268 error 4 in libscale.so[7f5ce70cb000+24000] 解决方法: 这些方法只能尝试,不一定

CMS .NET 程序框架 从2.0/3.5升级到4.0 版本后 需要调整的地方

问题一: document.forms1.action 不可使用 需要修改程 document.forms[0] .NET 程序框架 从2.0/3.5升级到4.0 版本后,document.forms1  不可使用 也就是说所有浏览器都是支持document.forms["form1"]这样的写法的. 微软4.0之前是document.form1也能获取, 升级到4.0之后 取值方式为document.forms["form1"] 或者document.forms[

就地升级WSUS 3.0 SP2 ON windows server 2008 R2 ENT 到windows server 2012 R2 S

目标: Windows server 2008 r2 系统standard 版本不支持数据重删除,而wsus所在盘想启用dedup ,虽然网上说大概只有17%的去重率,但是如果占用空间在240GB,17%的去重率还是节省不少空间的. Inplace 升级wsus 3.0 sp2 到wsus 6是不支持的,官方的步骤是安装另外一台wsus服务器,然后迁移数据库.组.WSUS下载的文件,更改wsus的Identifier,再更改客户端WSUS Server指向. 因为之前的WSUS还有其他服务,比如

vsphere5.5升级到6.0操作手册

1 通过USB启动盘升级 使用USB升级好处就是可以跨大版本升级,比如从5.0/5.1/5.5升级到6.0. 1) 首先你需要制作一个ESXi-6.0.0-2494585的USB启动盘(制作方法请参考在我的文库里找,这里不再讲解) 2) 修改BIOS启动顺序,让它从USB启动(在此之前请将该主机上的虚拟机都迁移到其他主机或关机,然后重启ESXI主机) 3) 开始升级,选择"upgrade ESXi, Preserve VMFS datastore" 2 通过Esxcli命令升级 通过S

gradle2.0笔记——让项目升级到gradle2.0

昨晚看到QQ群消息说gradle2.0发布了,今天去看了一下,确实是昨天发布的,为rc版本:Gradle 2.0-rc-2.于是决定试一下. gradle可以在官网上下载,地址如下:http://www.gradle.org/release-candidate.不过由于我许多项目因为兼容性问题目前暂时用不了gradle2.0,所以现在只能对我个别私人项目使用.如果你想让所有项目都用gradle2.0的话,可以下载gradle2.0然后解压,修改对应的环境变量指向到gradle2.0的目录.然后项

ADT for Eclipse无法升级到23.0的解决方法

最近一次的升级,ADT无法从ADT 22.X升级到23.0.2版本 需要重新卸载,然后重新安装即可 卸载方法: Help->Install new software->点击 页面右下角部分的: already installed ->选择要下载插件(ADT,一般都是以Android开头的)的即可 这应该是Google没有考虑兼容的问题,导致升级出错. 当然,你还可以重新下个Eclipse,然后重新安装ADT for Eclipse ADT for Eclipse无法升级到23.0的解决方

gitlab(7.9)升级到8.0.1

1.gitlab8.0更新说明 GitLab 8.0 现在完全集成了持续集成工具 (GitLab CI) ,此外还完全重写了 UI,节省了至少 50% 的磁盘空间.更快的合并,内置持续集成(CI)到 GitLab 本身,提高了界面和导航,以及“通过电子邮件回复”功能,它可以使用户通过移动设备就能够对某个问题上迅速发表评论,或者合并请求. GitLab 8.0 主要改进:更好的 HTTP 支持邮件快速回复Gmail 快速打开改善文件上传功能改进 MattermostWeb hooks 的 SSL

Android Studio中Android Support Library升级到 23.0.0 报错!

Android Support Library升级到 23.0.0  报错解决方案 不得不说,真能折腾!将Android Studio中SDK下的 Android Support Library 升级到了23.0.0,结果倒好了,创建项目出问题了,还比较奇葩: 卧槽你大爷!尼玛还能不能好好玩耍了.各种卸载重装,就差重装系统了. 后来在神器stackoverflow中找到了这么一篇: http://stackoverflow.com/questions/32092511/resource-erro