24

Web API (asp mvc4 アプリケーション内) の作成で忙しいです。ドキュメントを生成するために asp.net サイトで提案されているライブラリを使用しています ( http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages )。

私の問題は、パラメーターがモデルの場合、生成されたヘルプ ページでモデルに含まれるプロパティを指定できないことです。

次に例を示します。

モデル:

public class TestModel
{
    property String FirstName {get;set;}
    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> <-- this works
/// <param name="FirstName">This is the first name </param>  <-- doesn't work
/// <param name ="model.Surname">This is the surname</param> <-- doesn't work
public HttpResponseMessage Post(my.namespace.models.TestModel model)
{
  ...
}

モデルのパラメーターのみが生成されます。

ドキュメント用に生成された xml ドキュメントを調べたところ、他のパラメーターが追加されています。

<member name="my.namespace.api.Post(my.namespace.models.TestModel)">
     <summary>
         this is a test action
     </summary>
     <param name="model>this is the model</param>
     <param name="FirstName">This is the first name </param>
     <param name="model.Surname">This is the surname</param>
</member>

ただし、ヘルプ ページでは、パラメーター モデルのみが生成されます。

XMLからパラメータを取得するメソッドまでた​​どりました。

Collection<ApiDescription> apiDescriptions = config.Services.GetApiExplorer().ApiDescriptions;

これは、自動生成される HelpPageConfigurationExtentions.cs にあります。

私はこれに間違った方法でアプローチしていますか? 回避策を知っている人はいますか?

任意の提案や解決策をいただければ幸いです。

4

2 に答える 2

27

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>
}
于 2013-11-01T21:02:06.617 に答える
0

ジョサントの答えはうまくいきます。しかし、それは少し熱心すぎることがわかりました。文字列のような単純なものをモデルとして報告し、長さフィールドを持つ Char 配列であると報告していることがわかりました!

これはモデルにのみ必要だったので、次のコードを GetModelDocumentation メソッドの最後に追加しました。

if (parameterDescriptor.ParameterName == "value" || parameterDescriptor.ParameterName == "model")
{
    retDictionary.Add(parameterDescriptor.ParameterName, typeDocs);
}

これで、非単純型のパラメーターの詳細のみが返されます。

于 2013-12-17T04:22:03.840 に答える