MVC Web API 文档功能使用反射遍历您的 API 类和方法。这将构建文档的结构,但会导致或多或少的空(无用)文档,除非您添加了文档注释。
文档的正文使用 XML 文件填充,该文件使用 /// 文档注释生成,该文档注释具有必须遵循的特定结构。这意味着你不能用你希望它显示的任何东西填充你的 xml,它实际上必须连接到你的 API 中的东西,并且必须遵循你的类和属性的结构。
因此,在您的情况下,您不能将模型属性文档放在 api 方法中。您必须将其放入该属性所在的模型中。
模型:
public class TestModel
{
/// <summary>This is the first name </summary>
property String FirstName {get;set;}
/// <summary>This is the surname</summary>
property String Surname {get; set;}
property Boolean Active {get;set;}
}
行动:
/// <summary>
/// This is a test action
/// </summary>
/// <param name="model">this is the model</param>
public HttpResponseMessage Post(my.namespace.models.TestModel model)
{
...
}
修改帮助页面
自动生成的默认帮助页面不包括模型文档,仅记录了 api 方法。为了在您的 api 中显示有关参数的更多信息,需要进行自定义。以下说明是添加参数文档的一种方法。
在 Areas/HelpPage/Models 中创建两个新类型
public class TypeDocumentation
{
public TypeDocumentation()
{
PropertyDocumentation = new Collection<PropertyDocumentation>();
}
public string Summary { get; set; }
public ICollection<PropertyDocumentation> PropertyDocumentation { get; set; }
}
public class PropertyDocumentation
{
public PropertyDocumentation(string name, string type, string docs)
{
Name = name;
Type = type;
Documentation = docs;
}
public string Name { get; set; }
public string Type { get; set; }
public string Documentation { get; set; }
}
向 HelpPageApiModel.cs 添加新属性
public IDictionary<string, TypeDocumentation> ParameterModels{ get; set; }
创建一个新界面
internal interface IModelDocumentationProvider
{
IDictionary<string, TypeDocumentation> GetModelDocumentation(HttpActionDescriptor actionDescriptor);
}
修改 XmlDocumentationProvider 以实现新接口
public class XmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
{
private const string TypeExpression = "/doc/members/member[@name='T:{0}']";
private const string PropertyExpression = "/doc/members/member[@name='P:{0}']";
///...
///... existing code
///...
private static string GetPropertyName(PropertyInfo property)
{
string name = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", property.DeclaringType.FullName, property.Name);
return name;
}
public IDictionary<string, TypeDocumentation> GetModelDocumentation(HttpActionDescriptor actionDescriptor)
{
var retDictionary = new Dictionary<string, TypeDocumentation>();
ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor;
if (reflectedActionDescriptor != null)
{
foreach (var parameterDescriptor in reflectedActionDescriptor.GetParameters())
{
if (!parameterDescriptor.ParameterType.IsValueType)
{
TypeDocumentation typeDocs = new TypeDocumentation();
string selectExpression = String.Format(CultureInfo.InvariantCulture, TypeExpression, GetTypeName(parameterDescriptor.ParameterType));
var typeNode = _documentNavigator.SelectSingleNode(selectExpression);
if (typeNode != null)
{
XPathNavigator summaryNode;
summaryNode = typeNode.SelectSingleNode("summary");
if (summaryNode != null)
typeDocs.Summary = summaryNode.Value;
}
foreach (var prop in parameterDescriptor.ParameterType.GetProperties())
{
string propName = prop.Name;
string propDocs = string.Empty;
string propExpression = String.Format(CultureInfo.InvariantCulture, PropertyExpression, GetPropertyName(prop));
var propNode = _documentNavigator.SelectSingleNode(propExpression);
if (propNode != null)
{
XPathNavigator summaryNode;
summaryNode = propNode.SelectSingleNode("summary");
if (summaryNode != null) propDocs = summaryNode.Value;
}
typeDocs.PropertyDocumentation.Add(new PropertyDocumentation(propName, prop.PropertyType.Name, propDocs));
}
retDictionary.Add(parameterDescriptor.ParameterName, typeDocs);
}
}
}
return retDictionary;
}
}
在 GenerateApiModel 方法中将代码添加到 HelpPageConfigurationExtension
IModelDocumentationProvider modelProvider =
config.Services.GetDocumentationProvider() as IModelDocumentationProvider;
if (modelProvider != null)
{
apiModel.ParameterModels = modelProvider.GetModelDocumentation(apiDescription.ActionDescriptor);
}
修改 HelpPageApiModel.cshtml,将其添加到您希望显示模型文档的位置。
bool hasModels = Model.ParameterModels.Count > 0;
if (hasModels)
{
<h2>Parameter Information</h2>
@Html.DisplayFor(apiModel => apiModel.ParameterModels, "Models")
}
将 Models.cshtml 添加到 DisplayTemplates
@using System.Web.Http
@using System.Web.Http.Description
@using MvcApplication2.Areas.HelpPage.Models
@model IDictionary<string, TypeDocumentation>
@foreach (var modelType in Model)
{
<h3>@modelType.Key</h3>
if (modelType.Value.Summary != null)
{
<p>@modelType.Value.Summary</p>
}
<table class="help-page-table">
<thead>
<tr>
<th>Property</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach (var propInfo in modelType.Value.PropertyDocumentation)
{
<tr>
<td class="parameter-name"><b>@propInfo.Name</b> (@propInfo.Type)</td>
<td class="parameter-documentation">
<pre>@propInfo.Documentation</pre>
</td>
</tr>
}
</tbody>
</table>
}