前言: 你先得知道HelpPageConfig文件,不知道說明你現在不需要這個,所以下文就不用看了,等知道了再看也不急.當然如果你很知道這個,下文也不用看了,因為你會了. 方法一: 方法二: 自定義一個支持從目錄載入xml文檔的XmlDocumentationProvider 使用方法: 使用方法: ...
前言:
你先得知道HelpPageConfig文件,不知道說明你現在不需要這個,所以下文就不用看了,等知道了再看也不急.當然如果你很知道這個,下文也不用看了,因為你會了.
方法一:
new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/Documentation.xml"))
替換成
new XmlDocumentationProvider("PluginsFolder/*.xml")
改ctor函數讓他支持多個XML文檔
using System.Xml.Linq; using System.Xml.XPath; XDocument finalDoc = null; foreach (string file in Directory.GetFiles(@"PluginsFolder", "*.xml")) { if(finalDoc == null) { finalDoc = XDocument.Load(File.OpenRead(file)); } else { XDocument xdocAdditional = XDocument.Load(File.OpenRead(file)); finalDoc.Root.XPathSelectElement("/doc/members") .Add(xdocAdditional.Root.XPathSelectElement("/doc/members").Elements()); } } // Supply the navigator that rest of the XmlDocumentationProvider code looks for _documentNavigator = finalDoc.CreateNavigator();
方法二: 自定義一個支持從目錄載入xml文檔的XmlDocumentationProvider
public MultiXmlDocumentationProvider(string xmlDocFilesPath) { XDocument finalDoc = null; foreach (string file in Directory.GetFiles(xmlDocFilesPath, "*.xml")) { using (var fileStream = File.OpenRead(file)) { if (finalDoc == null) { finalDoc = XDocument.Load(fileStream); } else { XDocument xdocAdditional = XDocument.Load(fileStream); finalDoc.Root.XPathSelectElement("/doc/members") .Add(xdocAdditional.Root.XPathSelectElement("/doc/members").Elements()); } } } // Supply the navigator that rest of the XmlDocumentationProvider code looks for _documentNavigator = finalDoc.CreateNavigator(); }
使用方法:
config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/")));
方法三:給預設的XmlDocumentationProvider加一個ctor
public XmlDocumentationProvider(IEnumerable<string> documentPaths) { if (documentPaths.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(documentPaths)); } XDocument fullDocument = null; foreach (var documentPath in documentPaths) { if (documentPath == null) { throw new ArgumentNullException(nameof(documentPath)); } if (fullDocument == null) { using (var stream = File.OpenRead(documentPath)) { fullDocument = XDocument.Load(stream); } } else { using (var stream = File.OpenRead(documentPath)) { var additionalDocument = XDocument.Load(stream); fullDocument?.Root?.XPathSelectElement("/doc/members").Add(additionalDocument?.Root?.XPathSelectElement("/doc/members").Elements()); } } } _documentNavigator = fullDocument?.CreateNavigator(); }
使用方法:
var xmlPaths = new[] { HttpContext.Current.Server.MapPath("~/bin/Path.To.FirstNamespace.XML"), HttpContext.Current.Server.MapPath("~/bin/Path.To.OtherNamespace.XML") }; var documentationProvider = new XmlDocumentationProvider(xmlPaths); config.SetDocumentationProvider(documentationProvider);
相關文章:
Web Api Help Page XML comments from more than 1 files(http://stackoverflow.com/questions/22165724/web-api-help-page-xml-comments-from-more-than-1-files/22169357#22169357)
本文地址:
從多個XML文檔中讀取數據用於顯示webapi幫助文檔(http://www.cnblogs.com/shiningrise/p/XmlDocumentationProvider.html)