为了方面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 = 26 };
}
}
}

因为创建的是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);

}

}

}

接下来,编译运行调试起来,效果如下图。

 

Asp.net Web Api开发Help Page配置和扩展的更多相关文章

  1. Asp.net Web Api开发Help Page 添加对数据模型生成注释的配置和扩展

    在使用webapi框架进行接口开发的时候,编写文档会需要与接口同步更新,如果采用手动式的更新的话效率会非常低.webapi框架下提供了一种自动生成文档的help Page页的功能. 但是原始版本的效果 ...

  2. Asp.Net Web API开发微信后台

    如果说用Asp.Net开发微信后台是非主流,那么Asp.Net Web API的微信后台绝对是不走寻常路. 需要说明的是,本人认为Asp.Net Web API在开发很多不同的请求方法的Restful ...

  3. 水果项目第3集-asp.net web api开发入门

    app后台开发,可以用asp.net webservice技术. 也有一种重量级一点的叫WCF,也可以用来做app后台开发. 现在可以用asp.net web api来开发app后台. Asp.net ...

  4. ASP.NET Web API 2 之路由配置

    Ø  简介 ASP.NET Web API 路由配置也是必须掌握的技术点之一,要真正的完全掌握和理解它也是需要一定的过程的.不过,在平常的开发过程中,对它有基本的了解就足够了.因为我们主要关注点并不在 ...

  5. [目录]ASP.NET web api开发实战

    第一章:Restful web service v.s. RPC style web service 第二章:ASP.NET web api v.s. WCF v.s. ASP.NET web ser ...

  6. 创建 ASP.NET Web API的Help Page

    转:创建WEBAPI项目 转:添加测试API中的ASP.NET Web API帮助页面

  7. Asp.net Web Api开发(第四篇)Help Page配置和扩展

    https://blog.csdn.net/sqqyq/article/details/52708613

  8. asp.net web api 开发时应当注意的事项

    Self referencing when returning chain of objects. This can be solved using a design pattern called t ...

  9. Creating Help Pages for ASP.NET Web API -摘自网络

    When you create a web API, it is often useful to create a help page, so that other developers will k ...

随机推荐

  1. Eslint使用(webpack中使用)

    一.安装 npm i -D eslint npm i babel-eslint \ eslint-config-airbnb \ // Airbnb的编码规范是在业界非常流行的一套规范 eslint- ...

  2. gulp es6 转 es5

    npm install --save-dev gulp-babel babel-preset-es2015 var babel = require("gulp-babel"); / ...

  3. Vue2+VueRouter2+webpack 构建项目实战(一):准备工作

    环境准备 首先,要开始工作之前,还是需要把环境搭建好.需要的环境是nodejs+npm,当然现在安装node都自带了npm. 在终端下面输入命令node -v会有版本号出来.就说明安装成功了.输入np ...

  4. phpcms有二级导航并且高亮效果代码

    <div class="collapse navbar-collapse" id="example-navbar-collapse"> <ul ...

  5. CSS3动画属性:变形(transform)

    Transform字面上就是变形,改变的意思.在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix. 语法 t ...

  6. 29.Odoo产品分析 (四) – 工具板块(2) – 搜索和仪表盘(1)

    查看Odoo产品分析系列--目录 "项目管理"是一个用于管理你的项目,且将它们与其他应用关联起来的非常灵活的模块,他允许您的公司管理项目阶段,分配团队,甚至跟踪与项目相关的时间和工 ...

  7. 运行 python *.py 文件出错,如:python a.py

    运行 python *.py 文件出错,如:python a.py(下图) 原因:没有安装web.py 解决:下载并安装 网址:http://webpy.org/install#install  (h ...

  8. <自动化测试方案书>方案书目录排版

    自动化测试方案书 一.介绍 QQ交流群:585499566 这篇是一个系列,用来给需要做自动化测试方案的人做个参考,文章的内容是我收集网上和自己工作经验所得,希望能够给你们有所帮助 背景:因为工作需要 ...

  9. [转] Vue生命周期

    Vue生命周期 这是Vue文档里关于实例生命周期的解释图 那么下面我们来进行测试一下 <section id="app-8"> {{data}} </sectio ...

  10. android默认开启adb调试方法分析

    用adb调试android时,每次接入usb线,都会提示一个确认打开usb调试功能的窗口,有时候,我们需要默认打开usb调试功能.或者无需弹出对话框,直接默认开启.这个我们需要分析adb的流程了. a ...