C# HelpPage 接口文档配置
1、打开项目路径如下的类文件:
1.1、找类方法 Register 下的 config.SetDocumentationProvider 并取消注释,修改 ~/App_Data/XmlDocument.xml 为你自己的路径,
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data")))
1.2、打开 XmlDocumentationProvider 类文件
1.2.1、找到类中找到变量 _documentNavigator,修改如下所示
//private XPathNavigator _documentNavigator; private List<XPathNavigator> _documentNavigators = new List<XPathNavigator>();
1.2.2、找到类的方法 XmlDocumentationProvider ,修改如下:
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.3、在 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.4、修改 类中所有引用为 _documentNavigator.SelectSingleNode 的地方,修改成 新增的方法 SelectSingleNode
注意:新建的控制器必须继承 ApiController 否则界面不会展示控制器接口!
2、添加接口详情测试按钮
2.1、通过Nuget包管理添加 webapitestclient ,添加完成后找到这个 Areas->HelpPage->Views->Help 路径下的 Api.cshtml 文件,修改如下
@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>