WebApi 生成接口文檔

記錄下 WebApi 自動(dòng)生成接口文檔實(shí)現(xiàn)方法,Swagger 或者 HelpPage 都能很好實(shí)現(xiàn) 。這里使用HelpPage實(shí)現(xiàn)。

解決方案的結(jié)構(gòu)

解決方案有3個(gè)項(xiàng)目,主要用于分層:
1.webapi,引用service和model
2.service,引用model
3.model

HelPage 配置步驟:

1. 添加HelpPage

在webapi 中 nuget引入 Microsoft.AspNet.WebApi.HelpPage 包,正常情況下會(huì)自動(dòng)添加一個(gè)HelpPage的Area,如下圖:


效果圖

到此,已經(jīng)實(shí)現(xiàn)基本功能,能夠通過(guò)xxxx/help訪問(wèn)接口文檔了。

2. 為HepPage啟用測(cè)試功能

nuget引入 Web API Test Client。若API頁(yè)面右下角沒(méi)有出現(xiàn)TEST API按鈕,則參照如下代碼修改Api.cshtml:

@using System.Web.Http
@using WebApi.Areas.HelpPage.Models
@model HelpPageApiModel
@section Scripts {
    @Html.DisplayForModel("TestClientDialogs")
    @Html.DisplayForModel("TestClientReferences")
}

@{
    var description = Model.ApiDescription;
    ViewBag.Title = description.HttpMethod.Method + " " + description.RelativePath;
}

<link type="text/css" href="~/Areas/HelpPage/HelpPage.css" rel="stylesheet" />
<div id="body" class="help-page">
    <section class="featured">
        <div class="content-wrapper">
            <p>
                @Html.ActionLink("Help Page Home", "Index")
            </p>
        </div>
    </section>
    <section class="content-wrapper main-content clear-fix">
        @Html.DisplayForModel()
    </section>
</div>

效果界面

3.為API文檔顯示注釋。

默認(rèn)情況下 HelpPage 僅顯示Controller中的注釋,無(wú)法顯示其他項(xiàng)目的注釋,這是因?yàn)镠elpPage使用了項(xiàng)目的XML文檔文件,而WebApi中沒(méi)有Model和Service的XML文件,那么就為Model和Service添加X(jué)ml文檔文件。

首先,右鍵Model和Service項(xiàng)目,選擇屬性,在生成欄中找到輸出,勾選XML文檔文件,記住文件名


設(shè)置生成Xml文檔文件

然后,在HelpPage中新增MultiXmlDocumentationProvider類,用于讀取xml文件,代碼如下:

/// <summary>A custom 
    /// <see cref="IDocumentationProvider"/> 
    /// that reads the API documentation from a collection of XML documentation files.
    /// </summary>
    public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
    {
        /*********
    ** Properties
    *********/
        /// <summary>The internal documentation providers for specific files.</summary>
        private readonly XmlDocumentationProvider[] Providers;
        
        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="paths">The physical paths to the XML documents.</param>
        public MultiXmlDocumentationProvider(params string[] paths)
        {
            this.Providers = paths.Select(p => new XmlDocumentationProvider(p)).ToArray();
        }

        /// <summary>Gets the documentation for a subject.</summary>
        /// <param name="subject">The subject to document.</param>
        public string GetDocumentation(MemberInfo subject)
        {
            return this.GetFirstMatch(p => p.GetDocumentation(subject));
        }

        /// <summary>Gets the documentation for a subject.</summary>
        /// <param name="subject">The subject to document.</param>
        public string GetDocumentation(Type subject)
        {
            return this.GetFirstMatch(p => p.GetDocumentation(subject));
        }

        /// <summary>Gets the documentation for a subject.</summary>
        /// <param name="subject">The subject to document.</param>
        public string GetDocumentation(HttpControllerDescriptor subject)
        {
            return this.GetFirstMatch(p => p.GetDocumentation(subject));
        }

        /// <summary>Gets the documentation for a subject.</summary>
        /// <param name="subject">The subject to document.</param>
        public string GetDocumentation(HttpActionDescriptor subject)
        {
            return this.GetFirstMatch(p => p.GetDocumentation(subject));
        }

        /// <summary>Gets the documentation for a subject.</summary>
        /// <param name="subject">The subject to document.</param>
        public string GetDocumentation(HttpParameterDescriptor subject)
        {
            return this.GetFirstMatch(p => p.GetDocumentation(subject));
        }

        /// <summary>Gets the documentation for a subject.</summary>
        /// <param name="subject">The subject to document.</param>
        public string GetResponseDocumentation(HttpActionDescriptor subject)
        {
            return this.GetFirstMatch(p => p.GetDocumentation(subject));
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Get the first valid result from the collection of XML documentation providers.</summary>
        /// <param name="expr">The method to invoke.</param>
        private string GetFirstMatch(Func<XmlDocumentationProvider, string> expr)
        {
            return this.Providers
                .Select(expr)
                .FirstOrDefault(p => !String.IsNullOrWhiteSpace(p));
        }
    }

然后修改~/Area/HelpPage/App_Start/HelpPageConfig.cs的Register方法,如下:

            config.SetDocumentationProvider(
                new MultiXmlDocumentationProvider(
                    HttpContext.Current.Server.MapPath("~/bin/WebApi.xml")
                    , HttpContext.Current.Server.MapPath("~/bin/Service.xml")
                    , HttpContext.Current.Server.MapPath("~/bin/Model.xml")));

全部大功告成!

4.效果

實(shí)現(xiàn)效果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • Awesome DotNet,這又是一個(gè) Awesome XXX 系列的資源整理,由 quozd 發(fā)起和維護(hù)。內(nèi)容...
    小明yz閱讀 4,032評(píng)論 0 47
  • 需求: 為客戶端同事寫接口文檔的各位后端同學(xué),已經(jīng)在各種場(chǎng)合回憶了使用自動(dòng)化文檔工具前手寫文檔的血淚史.我的故事卻...
    _Lyux閱讀 4,940評(píng)論 0 2
  • IOC容器 Unity 微軟企業(yè)庫(kù)的基礎(chǔ),功能簡(jiǎn)單,擴(kuò)展方便,微軟官方提供一個(gè)EventBus的擴(kuò)展例子,值得一看...
    Bobby0322閱讀 1,039評(píng)論 0 6
  • 期待別人喜歡自己,期待自己可以利用一個(gè)方便簡(jiǎn)潔的方法讓人家喜歡自己,幻想世界是如何運(yùn)作,這都是自戀的幻覺(jué),妄想控制...
    封子末閱讀 178評(píng)論 0 0

友情鏈接更多精彩內(nèi)容