关于Web Api的HelpPage文档注释问题
之前使用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文件路径要根据自己项目的路径配置好。
关于Web Api的HelpPage文档注释问题的更多相关文章
- 关于ASP.NET Web Api的HelpPage文档注释问题
关于ASP.NET Web Api的HelpPage文档注释问题 以前我用微软的HelpPage来自动生成的webAPI帮助文档.在使用了一段时间后发现只能显示Controller上面写的注释文档内容 ...
- Effective Java 第三版——56. 为所有已公开的API元素编写文档注释
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- asp.net core web api 生成 swagger 文档
asp.net core web api 生成 swagger 文档 Intro 在前后端分离的开发模式下,文档就显得比较重要,哪个接口要传哪些参数,如果一两个接口还好,口头上直接沟通好就可以了,如果 ...
- ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档
原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...
- 第四十四条:为所有导出的API元素编写文档注释
简而言之,要为API编写文档,文档注释是最好,最有效的途径.对于所有可导出的API元素来说,使用文档注释应该被看作是强制性的.要 采用一致的风格来遵循标准的约定.记住,在文档注释内部出现任何的HTML ...
- 使用swagger实现web api在线接口文档
一.前言 通常我们的项目会包含许多对外的接口,这些接口都需要文档化,标准的接口描述文档需要描述接口的地址.参数.返回值.备注等等:像我们以前的做法是写在word/excel,通常是按模块划分,例如一个 ...
- 1.1 WEB API 在帮助文档页面进行测试
这篇文章http://www.cnblogs.com/landeanfen/p/5210356.html写得比较详细, 我就挑简单的来说. 首先用这功能要在WEB API创建的帮助文档下面,如果你使用 ...
- 使用swagger实现web api在线接口文档(转载)
一.前言 通常我们的项目会包含许多对外的接口,这些接口都需要文档化,标准的接口描述文档需要描述接口的地址.参数.返回值.备注等等:像我们以前的做法是写在word/excel,通常是按模块划分,例如一个 ...
- Web Api 多项目文档生成之SwaggerUI
SwaggerUI 可以生成不错的文档,但默认只能作用于单个api 项目,研究了一下源码发现只需修改一下SwaggerConfig.cs文件即可支持多API项目 1.使用生成脚本把xml文件复制到AP ...
随机推荐
- jquery live hover事件的替代写法
HTML中的hover行为通常在样式中定义,利用jquery实现此效果有两种情况. 第一种是常见的针对页面中静态的元素,以改变元素样式中的border-color为例,写法如下: $(function ...
- ASP.NET的WebConfig
转:http://blog.csdn.net/q3498233/article/details/8137364 WebConfig 花了点时间整理了一下ASP.NET Web.config配置文件的基 ...
- HTTP,TCP,Socket
TCP/IP三次握手和HTTP过程 1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络 ...
- UIScrollView 之图片缩放
UIScrollView 之图片缩放 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理 也就是说,要完成缩放功能的话,只 ...
- MySQL 删除数据库
MySQL 删除数据库 使用 mysqladmin 删除数据库 使用普通用户登陆mysql服务器,你可能需要特定的权限来创建或者删除 MySQL 数据库. 所以我们这边使用root用户登录,root用 ...
- JavaScript--函数-按值传递
按值传递(byValue): 两个变量间赋值或将变量作为参数传入函数时,其实都是将原变量中的值,赋值一份给对方(新变量) 对原始类型的值: 修改新变量,不会影响原变量 对引用类型的对象: 通过新变量修 ...
- 从头开始学c++,补基础,补踏实
在对c++一知半解的情况下,写c++程序是非常吃力的.对于半路出家写c++的我,写了几个颓废的程序后,再也没有勇气用现有的c++知识去写千疮百孔的程序.非常想写出<整洁的代码>中那样的代码 ...
- JavaScript的“闭包”到底是什么
在JavaScripot函数闭包的定义中,一般都有一个outer 函数,一个inner函数.那么“闭包”到底是指outer函数呢,还是指inner函数? 从官方定义来看,并不清楚:A closure ...
- 【USACO 3.1.4】形成的区域
[描述] N个不同的颜色的不透明的长方形(1 <= N <= 1000)被放置在一张宽为A长为B的白纸上.这些长方形被放置时,保证了它们的边于白纸的边缘平行.所有的长方形都 ...
- SxsTrace工具使用方法(转)
http://blog.sina.com.cn/s/blog_494e45fe0102dtt3.html Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库 ...