当前位置: 首页 > news >正文

4、使用WebAPITestClient

一、使用NuGet安装WebAPITestClient

注:可以是单独的项目,也可以安装于Api项目中,下面以安装于Api项目中为例

1、Api 右键管理Nuget

2、安装WebAPITestClient

3、检查XmlDocumentationProvider.cs(以下为调试正常的程序)

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;

namespace WebApiTestClient.Areas.HelpPage
{
    /// <summary>
    /// A custom <see cref="IDocumentationProvider"/> that reads the API documentation from an XML documentation file.
    /// </summary>
    public class XmlDocumentationProvider : IDocumentationProvider
    {
        private XPathNavigator _documentNavigator;
        private const string MethodExpression = "/doc/members/member[@name='M:{0}']";
        private const string ParameterExpression = "param[@name='{0}']";
        private const string TypeExpression = "/doc/members/member[@name='T:{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 virtual string GetDocumentation(HttpActionDescriptor actionDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(actionDescriptor);
            if (methodNode != null)
            {
                XPathNavigator summaryNode = methodNode.SelectSingleNode("summary");
                if (summaryNode != null)
                {
                    return summaryNode.Value.Trim();
                }
            }

            return null;
        }

        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;
        }

        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}", method.DeclaringType.FullName, 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 GetTypeName(Type type)
        {
            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 typeName = genericType.FullName;

                // Trim the generic parameter counts from the name
                typeName = typeName.Substring(0, typeName.IndexOf('`'));
                string[] argumentTypeNames = genericArguments.Select(t => GetTypeName(t)).ToArray();
                return String.Format(CultureInfo.InvariantCulture, "{0}{{{1}}}", typeName, String.Join(",", argumentTypeNames));
            }

            return type.FullName;
        }

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

        public string GetResponseDocumentation(HttpActionDescriptor actionDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(actionDescriptor);
            if (methodNode != null)
            {
                XPathNavigator summaryNode = methodNode.SelectSingleNode("summary");
                if (summaryNode != null)
                {
                    return summaryNode.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 GetTagValue(XPathNavigator parentNode, string tagName)
        {
            if (parentNode != null)
            {
                XPathNavigator node = parentNode.SelectSingleNode(tagName);
                if (node != null)
                {
                    return node.Value.Trim();
                }
            }

            return null;
        }
    }
}
View Code
二、配置注释文档Xml的生成路径

1、Api项目

2、在生成页中设置输出路径

 

转载于:https://www.cnblogs.com/su1643/p/7183518.html

相关文章:

  • 王自如与老罗的辩论赛谁赢了?!
  • 01背包模板
  • jQuery的masonry插件实现瀑布流布局
  • Mongo基本使用:
  • “可信网站”真的可信吗?
  • 安装wdcp linux一键安装包云系统客户端教程
  • HDU 5547 Sudoku(DFS)
  • FreeRTOS 消息队列
  • lintcode-109-数字三角形
  • 关于python ide
  • git学习
  • SVM
  • Java编码格式
  • 【SignalR学习系列】2. 第一个SignalR程序
  • Passing the Message 单调栈两次
  • [nginx文档翻译系列] 控制nginx
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • 5、React组件事件详解
  • javascript面向对象之创建对象
  • JSDuck 与 AngularJS 融合技巧
  • Mac转Windows的拯救指南
  • mockjs让前端开发独立于后端
  • MYSQL 的 IF 函数
  • Netty 4.1 源代码学习:线程模型
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • orm2 中文文档 3.1 模型属性
  • PAT A1050
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • vagrant 添加本地 box 安装 laravel homestead
  • vue.js框架原理浅析
  • 编写高质量JavaScript代码之并发
  • 大快搜索数据爬虫技术实例安装教学篇
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 聊聊redis的数据结构的应用
  • 你不可错过的前端面试题(一)
  • 少走弯路,给Java 1~5 年程序员的建议
  • 思维导图—你不知道的JavaScript中卷
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • #1015 : KMP算法
  • #14vue3生成表单并跳转到外部地址的方式
  • #考研#计算机文化知识1(局域网及网络互联)
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (1)常见O(n^2)排序算法解析
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (HAL库版)freeRTOS移植STMF103
  • (Matlab)使用竞争神经网络实现数据聚类
  • (二)Eureka服务搭建,服务注册,服务发现
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .net 提取注释生成API文档 帮助文档
  • .Net 中Partitioner static与dynamic的性能对比
  • @Bean注解详解