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 のパラメーターに関する詳細情報を表示するには、カスタマイズが必要です。以下の手順は、パラメーター ドキュメントを追加する 1 つの方法です。
Areas/HelpPage/Models に 2 つの新しいタイプを作成します。
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>
}