之前使用Microsoft.AspNet.WebApi.HelpPage的时候,一直为返回对象的注释发愁,以为这是个BUG。

这个注释的解决办法其实要从其原理理解就明白了。

因为HelpPage是读取的XML文件生成的,你的对象没有生成XML文件,当然显示的是空的。

那么解决的办法如下,

1.将你的返回对象所在的库也生成XML文件,右键属性--生成。都选XML项,配置好XML文件生成路径。

2.在原有的基础上,写一个读取多个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));
}
}

3.替换HelpPageConfig里面的读取方法:

//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml"), HttpContext.Current.Server.MapPath("~/App_Data/Api.xml")));
Api.xml为你生成的对象XML文件。

然后重新生成就OK了。

当然生成的XML文件路径要根据自己项目的路径配置好。

最新文章

  1. Nginx配置文件nginx.conf中文详解
  2. LEfSe分析
  3. 如何利用.snk文件生成DLL文件中的Publickeytoken
  4. javascript 命令方式 测试例子
  5. C 文件读写1
  6. C#去除字符串的最后一个字符
  7. 用systemtap对sysbench IO测试结果的分析1
  8. JVM 关闭前执行命令的钩子
  9. 2337:[HNOI2011]XOR和路径 - BZOJ
  10. C# 编写短信发送Window服务
  11. A Statistical View of Deep Learning (IV): Recurrent Nets and Dynamical Systems
  12. c#跟objective-c语言特性
  13. JSON的使用小结
  14. 自学Zabbix3.6.2-触发器triggers severity严重程度
  15. 函数round和trunc
  16. autolayout 高度自适应
  17. 更改配置:远程访问gitlab的postgresql数据库
  18. Java中创建对象的四种方法
  19. 【环境变量】Linux 下三种方式设置环境变量与获取环境变量
  20. 网络IO之阻塞、非阻塞、同步、异步总结【转】

热门文章

  1. C#中创建、打开、读取、写入、保存Excel的一般性代码
  2. Ext.ComponentQuery.query()
  3. 使用less函数实现不同背景的CSS样式
  4. 【Usaco2008 Mar】土地购买
  5. sublime常用插件及配置,自留自用
  6. iphone升级ios7之后出现蓝框框一直跳的问题
  7. Scut:Redis 资源管理器
  8. The working copy xxxx needs to be upgraded to Subversion 1.7.
  9. Java 的性能优化
  10. 转:为什么要使用NoSQL