為了方面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); } } }

