Lev*_*van 22 asp.net documentation asp.net-web-api asp.net-web-api-helppages
我的Web api项目中有不同的插件和自己的XML文档,并且有一个集中的帮助页面,但问题是Web Api的默认帮助页面仅支持单个文档文件
new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/Documentation.xml"))
Run Code Online (Sandbox Code Playgroud)
如何从不同的文件加载配置?我想这样做:
new XmlDocumentationProvider("PluginsFolder/*.xml")
Run Code Online (Sandbox Code Playgroud)
Kir*_*lla 31
您可以修改已安装XmlDocumentationProvider
的Areas\HelpPage
以执行以下操作:
将多个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();
Run Code Online (Sandbox Code Playgroud)
gur*_*777 21
Kirans解决方案非常有效.我最终使用他的方法,但创建了一个名为MultiXmlDocumentationProvider的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();
}
Run Code Online (Sandbox Code Playgroud)
我从HelpPageConfig.cs注册新的提供程序:
config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/")));
Run Code Online (Sandbox Code Playgroud)
升级等时,创建一个新类并保持原始类不变可能更方便...
我没有在XmlMultiDocumentationProvider的行中创建一个单独的类,而是在现有的XmlDocumentationProvider中添加了一个构造函数.这需要一个字符串列表,而不是获取文件夹名称,因此您仍然可以准确指定要包含的文件(如果文档XML所在的目录中有其他xml文件,则可能会变得毛茸茸).这是我的新构造函数:
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();
}
Run Code Online (Sandbox Code Playgroud)
HelpPageConfig.cs看起来像这样.(是的,它可以是更少的行,但我没有行限制所以我喜欢拆分它.)
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);
Run Code Online (Sandbox Code Playgroud)
小智 5
我同意 gurra777 的观点,即创建新类是更安全的升级路径。我从该解决方案开始,但它涉及大量的复制/面食,在几次软件包更新后很容易过时。
相反,我保留了一个XmlDocumentationProvider
孩子的集合。对于每个实现方法,我都会调用子级来获取第一个非空结果。
public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
{
private IList<XmlDocumentationProvider> _documentationProviders;
public MultiXmlDocumentationProvider(string xmlDocFilesPath)
{
_documentationProviders = new List<XmlDocumentationProvider>();
foreach (string file in Directory.GetFiles(xmlDocFilesPath, "*.xml"))
{
_documentationProviders.Add(new XmlDocumentationProvider(file));
}
}
public string GetDocumentation(System.Reflection.MemberInfo member)
{
return _documentationProviders
.Select(x => x.GetDocumentation(member))
.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
}
//and so on...
Run Code Online (Sandbox Code Playgroud)
HelpPageConfig 注册与 gurra777 的回答相同,
config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/")));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6768 次 |
最近记录: |