为了方面APP开发人员,服务端的接口都应当提供详尽的API说明。但每次有修改,既要维护代码,又要维护文档,一旦开发进度紧张,很容易导致代码与文档不一致。

Web API有一个Help Page插件,可以很方便的根据代码及注释自动生成相关API说明页面。

Help Page安装步骤及扩展(以VS2015为例):

右键点击WebAPI项目的引用,选择"管理NuGet程序包"

在搜索框中输入 helppage进行搜索,结果如下图:

然后在右侧边栏点击安装按钮即可进行插件安装了。

安装完成后,你会发现项目下多了不少文件:

接下来,我们对Areas/HelpPage/App_Start/HelpPageConfig.cs进行改造。

改造前,我们需要先了解下HelpPageConfig.cs,其中的Register方法是用于注册Help Page页面需要展示的API的文档的。默认情况下,该方法只支持单个文档导入,所以我们需要扩展下。

我们创建一个可多文件注册的类:

using System;
using System.Linq;
using System.Reflection;
using System.Web.Http.Controllers;
using System.Web.Http.Description;
using WebApplication2.Areas.HelpPage.ModelDescriptions; namespace WebApplication2.Areas.HelpPage.App_Start
{
public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
{
private readonly XmlDocumentationProvider[] Providers;
public MultiXmlDocumentationProvider(params string[] paths)
{
this.Providers = paths.Select(p => new XmlDocumentationProvider(p)).ToArray();
} public string GetDocumentation(MemberInfo subject)
{
return this.GetFirstMatch(p => p.GetDocumentation(subject));
} public string GetDocumentation(Type subject)
{
return this.GetFirstMatch(p => p.GetDocumentation(subject));
} public string GetDocumentation(HttpControllerDescriptor subject)
{
return this.GetFirstMatch(p => p.GetDocumentation(subject));
} public string GetDocumentation(HttpActionDescriptor subject)
{
return this.GetFirstMatch(p => p.GetDocumentation(subject));
} public string GetDocumentation(HttpParameterDescriptor subject)
{
return this.GetFirstMatch(p => p.GetDocumentation(subject));
} public string GetResponseDocumentation(HttpActionDescriptor subject)
{
return this.GetFirstMatch(p => p.GetDocumentation(subject));
} private string GetFirstMatch(Func<XmlDocumentationProvider, string> expr)
{
return this.Providers
.Select(expr)
.FirstOrDefault(p => !String.IsNullOrWhiteSpace(p));
}
}
}

然后重写HelpPageConfig.cs文件中的代码如下:

using System.Diagnostics.CodeAnalysis;
using System.Web;
using System.Web.Http;
using WebApplication2.Areas.HelpPage.App_Start; namespace WebApplication2.Areas.HelpPage
{
public static class HelpPageConfig
{
[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
MessageId = "WebApplication2.Areas.HelpPage.TextSample.#ctor(System.String)",
Justification = "End users may choose to merge this string with existing localized resources.")]
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly",
MessageId = "bsonspec",
Justification = "Part of a URI.")]
public static void Register(HttpConfiguration config)
{
config.SetDocumentationProvider(new MultiXmlDocumentationProvider(
HttpContext.Current.Server.MapPath("~/bin/WebApplication2.XML")));
}
}
}

这里要注意下WebApplication2.XML,这个文件是需要我们对相关项目属性进行设置下的,让其生成相关xml文件。

然后我们来创建一个Controller用于测试。

using System.Web.Http;  

namespace WebApplication2.Controllers
{
/// <summary>
/// 测试响应对象
/// </summary>
public struct TestResponse {
/// <summary>
/// 姓名
/// </summary>
public string Name;
/// <summary>
/// 年龄
/// </summary>
public int Age;
} /// <summary>
/// 测试
/// </summary>
public class TestController : ApiController
{
/// <summary>
/// 测试接口
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("api/300/1000")]
public TestResponse JustTest()
{
return new TestResponse() { Name = "测试员", Age = };
}
}
}

因为创建的是Web API项目,所以这里还要修改下Global.asax,注册Area。

using System.Web.Http;
using System.Web.Mvc; namespace WebApplication2
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}

原文地址: https://blog.csdn.net/sqqyq/article/details/52708613#

Web Api HelpPage的更多相关文章

  1. 关于ASP.NET Web Api的HelpPage文档注释问题

    关于ASP.NET Web Api的HelpPage文档注释问题 以前我用微软的HelpPage来自动生成的webAPI帮助文档.在使用了一段时间后发现只能显示Controller上面写的注释文档内容 ...

  2. Web API使用记录系列(二)HelpPage优化与WebApiTestClient

    继续使用记录的第二节,HelpPage的优化与测试工具WebApiTestClient的使用. 之前没怎么整理博客,都是记录一下笔记,真正好好整理发现没想像的那么简单.不管怎么说还是培养下写博客的习惯 ...

  3. 我这么玩Web Api(一):帮助页面或用户手册(Microsoft and Swashbuckle Help Page)

    前言 你需要为客户编写Api调用手册?你需要测试你的Api接口?你需要和前端进行接口对接?那么这篇文章应该可以帮到你.本文将介绍创建Web Api 帮助文档页面的两种方式,Microsoft Help ...

  4. asp.net web api 的版本升级到 2.2的记录

    asp.net web api 的版本 升级到 2.2的记录 asp.net web api 2.2相比1.0提升了不少 而且其中最重要的就是有了在线文档的自动字段注释的功能 再也不用写详细的字段说明 ...

  5. Web API返回JSON数据

    对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...

  6. web api Route属性定义

    ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...

  7. Web API 自动生成帮助文档并使用Web API Test Client 测试

    之前在项目中有用到webapi对外提供接口,发现在项目中有根据webapi的方法和注释自动生成帮助文档,还可以测试webapi方法,功能很是强大,现拿出来与大家分享一下. 先看一下生成的webapi文 ...

  8. 为 ASP.NET Web API 创建帮助页面(转载)

    转载地址:http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages 当创建web API 时,经常要创 ...

  9. asp.net web api 测试帮助页面建立并测试

    asp.net web api 测试帮助页面建立并测试 现在使用WEB API来开发,越来越流行. 在开发过程中的测试调试,可以使用Fiddler等工具来帮助测试外,还有: 在asp.net 中有种方 ...

随机推荐

  1. haskell简明入门(一)

    本文的主要内容参考自<Haskell趣学指南> 1. What is Haskell?     以下内容引用自Haskell官网: Haskell是一个先进的,纯粹的函数式编程语言.一个典 ...

  2. shell 输入不显示在监视器上

    #!/bin/bash read -s -p "Enter your password:" pass echo "your password is $pass" ...

  3. ubuntu 14.04 (desktop amd 64) 查看配置参数

    硬盘型号 sudo hdparm -i /dev/sda |grep "Model"   硬盘数量大小 sudo fdisk -l |grep "Disk /dev/sd ...

  4. 这是一份很详细的 Retrofit 2.0 使用教程(含实例讲解)

    前言 在Andrroid开发中,网络请求十分常用 而在Android网络请求库中,Retrofit是当下最热的一个网络请求库 今天,我将献上一份非常详细Retrofit v2.0的使用教程,希望你们会 ...

  5. UVALive-3972 March of the Penguins (最大流:节点容量)

    题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...

  6. python自动化运维之路2

    list列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 #!/usr/bin/env python # _*_ encoding:utf-8 _*_ # a ...

  7. Scrum冲刺博客(总)

    一.在 Alpha 阶段认领的任务 二.明日任务安排 成员 任务 任务量/小时 陈伟泽 项目进度把控 1 李嘉廉 24点算法" Stack数据结构实现,生成不重复的算式(考虑结合律和结合律) ...

  8. pixi之动画

    一.循环动画 let sprite; Loader.add("images/imgs.json").load(setup); function setup() { //利用oran ...

  9. CS231n课程笔记翻译9:卷积神经网络笔记

    译者注:本文翻译自斯坦福CS231n课程笔记ConvNet notes,由课程教师Andrej Karpathy授权进行翻译.本篇教程由杜客和猴子翻译完成,堃堃和李艺颖进行校对修改. 原文如下 内容列 ...

  10. 简单CSS3代码实现立方体以及3D骰子

    1 实现3D立方体 首先准备好UL以及6个Li: 代码如下 ul { position: absolute; top: 50%; left: 50%; transform:translate(-50% ...