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 know how to call your API. You could create all of the documentation manually, but it is better to autogenerate as much as possible.
To make this task easier, ASP.NET Web API provides a library for auto-generating help pages at run time.

Creating API Help Pages
Install ASP.NET and Web Tools 2012.2 Update. This update integrates help pages into the Web API project template.
Next, create a new ASP.NET MVC 4 project and select the Web API project template. The project template creates an example API controller named ValuesController. The template also creates the API help pages. All of the code files for the help page are placed in the Areas folder of the project.

When you run the application, the home page contains a link to the API help page. From the home page, the relative path is /Help.

This link brings you to an API summary page.

The MVC view for this page is defined in Areas/HelpPage/Views/Help/Index.cshtml. You can edit this page to modify the layout, introduction, title, styles, and so forth.
The main part of the page is a table of APIs, grouped by controller. The table entries are generated dynamically, using the IApiExplorer interface. (I'll talk more about this interface later.) If you add a new API controller, the table is automatically updated at run time.
The “API” column lists the HTTP method and relative URI. The “Description” column contains documentation for each API. Initially, the documentation is just placeholder text. In the next section, I'll show you how to add documentation from XML comments.
Each API has a link to a page with mroe detailed information, including example request and response bodies.

Adding Help Pages to an Existing Project
You can add help pages to an existing Web API project by using NuGet Package Manager. This option is useful you start from a different project template than the “Web API” template.
From the Tools menu, select Library Package Manager, and then select Package Manager Console. In the Package Manager Console window, type one of the following commands:
For a C# application: Install-Package Microsoft.AspNet.WebApi.HelpPage
For a Visual Basic application: Install-Package Microsoft.AspNet.WebApi.HelpPage.VB
There are two packages, one for C# and one for Visual Basic. Make sure to use the one that matches your project.
This command installs the necessary assemblies and adds the MVC views for the help pages (located in the Areas/HelpPage folder). You'll need to manually add a link to the Help page. The URI is /Help. To create a link in a razor view, add the following:
@Html.ActionLink("API", "Index", "Help", new { area = "" }, null)
Also, make sure to register areas. In the Global.asax file, add the following code to the Application_Start method, if it is not there already:
protected void Application_Start()
{
// Add this code, if not present.
AreaRegistration.RegisterAllAreas(); // ...
}
Adding API Documentation
By default, the help pages have placeholder strings for documentation. You can use XML documentation comments to create the documentation. To enable this feature, open the file Areas/HelpPage/App_Start/HelpPageConfig.cs and uncomment the following line:
config.SetDocumentationProvider(new XmlDocumentationProvider(
HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
Now enable XML documentation. In Solution Explorer, right-click the project and select Properties. Select the Build page.

Under Output, check XML documentation file. In the edit box, type “App_Data/XmlDocument.xml”.

Next, open the code for the ValuesController API controller, which is defined in /Controllers/ValuesControler.cs. Add some documentation comments to the controller methods. For example:
/// <summary>
/// Gets some very important data from the server.
/// </summary>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
} /// <summary>
/// Looks up some data by ID.
/// </summary>
/// <param name="id">The ID of the data.</param>
public string Get(int id)
{
return "value";
}
Tip: If you position the caret on the line above the method and type three forward slashes, Visual Studio automatically inserts the XML elements. Then you can fill in the blanks.
Now build and run the application again, and navigate to the help pages. The documentation strings should appear in the API table.

The help page reads the strings from the XML file at run time. (When you deploy the application, make sure to deploy the XML file.)
Under the Hood
The help pages are built on top of the ApiExplorer class, which is part of the Web API framework. The ApiExplorer class provides provides the raw material for creating a help page. For each API, ApiExplorer contains an ApiDescription that describes the API. For this purpose, an "API" is defined as the combination of HTTP method and relative URI. For example, here are some distinct APIs:
- GET /api/Products
- GET /api/Products/{id}
- POST /api/Products
If a controller action supports multiple HTTP methods, the ApiExplorer treats each method as a distinct API.
To hide an API from the ApiExplorer, add the ApiExplorerSettings attribute to the action and set IgnoreApi to true.
[ApiExplorerSettings(IgnoreApi=true)]
public HttpResponseMessage Get(int id) { }
You can also add this attribute to the controller, to exclude the entire controller.
The ApiExplorer class gets documentation strings from the IDocumentationProvider interface. As you saw earlier, the Help Pages library provides an IDocumentationProvider that gets documentation from XML documentation strings. The code is located in /Areas/HelpPage/XmlDocumentationProvider.cs. You can get documentation from another source by writing your own IDocumentationProvider. To wire it up, call the SetDocumentationProvider extension method, defined in HelpPageConfigurationExtensions
ApiExplorer automatically calls into the IDocumentationProvider interface to get documentation strings for each API. It stores them in the Documentation property of the ApiDescription and ApiParameterDescription objects.
Next Steps
You aren't limited to the help pages shown here. In fact, ApiExplorer is not limited to creating help pages. Yao Huang Lin has written some great blog posts to get you thinking out of the box:
- Adding a simple Test Client to ASP.NET Web API Help Page
- Making ASP.NET Web API Help Page work on self-hosted services
- Design-time generation of help page (or client) for ASP.NET Web API
- Advanced Help Page customizations
Creating Help Pages for ASP.NET Web API -摘自网络的更多相关文章
- 使用ASP.NET Web API Help Pages 创建在线接口文档
操作步骤 1.新建Web API项目 2.在项目Areas文件夹下找到以下文件,取消注释图中代码. 3.右键解决方案,属性,如图设置. 4.运行程序,点击右上角API 接口列表: 详情-无参数: 详情 ...
- Professional C# 6 and .NET Core 1.0 - Chapter 42 ASP.NET Web API
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处: -------------------------------------------------------- ...
- Creating the Help Page in ASP.NET Web API
Introduction In this article we will define the process of creating the help page in the ASP .NET We ...
- Announcing the Release of ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 for VS2012
The NuGet packages for ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 are now live o ...
- Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)
前言 很久没更新博客了,加上刚过年,现在准备重新开战,继续自己的学习之路.本文已同步到Web API2系列文章中http://www.cnblogs.com/aehyok/p/3446289.html ...
- [转]Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)
本文转自:http://www.cnblogs.com/aehyok/p/3545824.html 前言 很久没更新博客了,加上刚过年,现在准备重新开战,继续自己的学习之路.本文已同步到Web API ...
- Getting Started with ASP.NET Web API 2 (C#)
By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print Download Com ...
- ASP.NET Web API与Rest web api(一)
HTTP is not just for serving up web pages. It is also a powerful platform for building APIs that exp ...
- ASP.NET Web API与Rest web api(一)
本文档内容大部分来源于:http://www.cnblogs.com/madyina/p/3381256.html HTTP is not just for serving up web pages. ...
随机推荐
- Linux使用者管理(1)---用户账号
linux很重要的应用就是作为服务器的操作系统.服务器的作用是给多用户提供各种“服务”(可能是读服务器上的文件,或者是利用服务器进行数值计算)那么如果多用户共同拥有一台服务器,就需要对服务器上的用户进 ...
- 【原创】中文分词系统 ICTCLAS2015 的JAVA封装和多线程执行(附代码)
本文针对的问题是 ICTCLAS2015 的多线程分词,为了实现多线程做了简单的JAVA封装.如果有需要可以自行进一步封装其它接口. 首先ICTCLAS2015的传送门(http://ictclas. ...
- iosUITextField属性
@property UITextField *caption; caption = [[UITextField alloc] initWithFrame:CGRectMake(, self.frame ...
- 用git difff 生成补丁
http://stackoverflow.com/questions/1191282/how-to-see-the-changes-between-two-commits-without-commit ...
- Linux查看所有用户用什么命令
用过Linux系统的人都知道,Linux系统查看用户不是会Windows那样,鼠标右键看我的电脑属性,然后看计算机用户和组即可. 那么Linux操作系统里查看所有用户该怎么办呢?用命令.其实用命令就能 ...
- hdu 4920 Matrix multiplication (矩阵计算)
题目链接 题意:给两个矩阵a, b, 计算矩阵a*b的结果对3取余. 分析:直接计算时间复杂度是O(n^3),会超时,但是下面第一个代码勉强可以水过,数据的原因. #include <iostr ...
- POJ 3253 Fence Repair【二叉堆】
题意:给出n根木板,需要把它们连接起来,每一次连接的花费是他们的长度之和,问最少需要多少钱. 和上一题果子合并一样,只不过这一题用long long 学习的手写二叉堆的代码,再好好理解= = #inc ...
- LeetCode Reverse Linked List (反置链表)
题意: 将单恋表反转. 思路: 两种方法:迭代和递归. 递归 /** * Definition for singly-linked list. * struct ListNode { * int va ...
- poj 2184 Cow Exhibition
// 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...
- 将Temporary文件夹里的Logo文件转移到Logo文件夹
/// <summary> /// 将Temporary文件夹里的Logo文件转移到Logo文件夹 /// </summary> /// <param name=&quo ...