C# HelpPage 接口文檔配置

一、打開項目路徑以下的類文件:

1.一、找類方法 Register 下的 config.SetDocumentationProvider 並取消註釋,修改 ~/App_Data/XmlDocument.xml  爲你本身的路徑,
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data")))
 1.二、打開 XmlDocumentationProvider 類文件

 1.2.一、找到類中找到變量 _documentNavigator,修改以下所示
 //private XPathNavigator _documentNavigator;
   private List<XPathNavigator> _documentNavigators = new List<XPathNavigator>();

1.2.二、找到類的方法 XmlDocumentationProvider ,修改以下:css

 public XmlDocumentationProvider(string documentPath)
        {
            if (documentPath == null)
            {
                throw new ArgumentNullException("documentPath");
            }
            //註釋這兩行
            //XPathDocument xpath = new XPathDocument(documentPath);
            //_documentNavigator = xpath.CreateNavigator();
            //新增以下
            DirectoryInfo theFolder = new DirectoryInfo(documentPath);
            foreach (var item in theFolder.GetFiles("*.xml"))
            {
                XPathDocument xpath = new XPathDocument(Path.Combine(documentPath, item.Name));
                _documentNavigators.Add(xpath.CreateNavigator());
            }
        }
1.2.三、在 XmlDocumentationProvider 方法以後添以下方法
 private XPathNavigator SelectSingleNode(string selectExpression) {
            foreach (var navigator in _documentNavigators) {
                var propertyNode = navigator.SelectSingleNode(selectExpression);
                if (propertyNode != null)
                    return propertyNode;
            }
            return null;
        }
 1.2.四、修改 類中全部引用爲 _documentNavigator.SelectSingleNode 的地方,修改爲 新增的方法 SelectSingleNode

注意:新建的控制器必須繼承  ApiController  不然界面不會展現控制器接口!html

 二、添加接口詳情測試按鈕

2.一、經過Nuget包管理添加 webapitestclient 添加完成後找到這個 Areas->HelpPage->Views->Help 路徑下的  Api.cshtml 文件,修改以下web

@using System.Web.Http
@using API.Areas.HelpPage.Models
@model HelpPageApiModel

@*解析html*@
@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>
相關文章
相關標籤/搜索