为了方面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. 【Linux命令】grep命令

    1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...

  2. js 控制超出字数显示省略号

    //多余显示省略号 function wordlimit(cname, wordlength) { var cname = document.getElementsByClassName(cname) ...

  3. jQuery效果之封装模拟placeholder效果,让低版本浏览器也支持

    页面中的输入框默认的提示文字一般使用placeholder属性就可以了,即: <input type="text" name="username" pla ...

  4. 使用FileReader接口读取文件内容

    如果想要读取或浏览文件,则需要通过FileReader接口,该接口不仅可以读取图片文件,还可以读取文本或二进制文件,同时,根据该接口提供的事件与方法,可以动态侦察文件读取时的详细状态,接下来,我们详细 ...

  5. Android为TV端助力 运算符&,|,^

    1.&按位“与”的计算是把两个数字分别写成二进制形式,然后按照每一位进行比较,&计算中,只要有一个是0就算成02.|运算转换成2进制进行比较,两个位只要有一个为1,那么结果就是1,否则 ...

  6. C# Partial:分部方法和分部类

    using System; namespace Partial { class Program { static void Main(string[] args) { A a = new A(); } ...

  7. (后台)jxl.read.biff.BiffException: Unable to recognize OLE stream

    在excel中打开,另存成xls就可以.

  8. python leetcode 字符串相乘

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

  9. SQL中常用日期函数

    --1 GETDATE() 返回当前系统日期SELECT GETDATE() --2 DATEADD(日期部分,常数,日期) 返回将日期的指定日期部分加常数后的结果返回 日期部分可以是: --常数为正 ...

  10. spring4笔记----spring4国际化

    <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www. ...