【问题标题】:VS generated help pages for WebAPI, property documentationVS 生成的 WebAPI 帮助页面,属性文档
【发布时间】:2016-07-23 23:36:22
【问题描述】:

我正在为 Web API 应用程序使用 Visual Studio 2013 自动生成的 XML 文档,它运行良好,除了从 List 继承的业务对象的属性;我从他们那里得到的只是“对象的集合”。

例如,下面是一个包含 OrderLineCollection 属性的 Order 对象:

public class Order 
{
    public OrderLineCollection Lines { get; set; }
}

public class OrderLine
{
    public string OrderNo { get; set; }
}

public class OrderLineCollection : List<OrderLine>
{
    public void ReadFromServer(string orderNo)
    {}
}

为 Order 对象生成的文档在 Lines 属性的 Type 列中只有“对象集合”,没有指向 OrderLine 对象的链接。

如果我像这样定义 Lines 属性,它可以工作(=我在 Type 列中得到“Collection of OrderLine”,OrderLine 超链接):

public class Order 
{
    public List<OrderLine> Lines { get; set; }
}

但是,我希望能够使用上面的 OrderLineCollection 类,这样我就可以在那里保留我的集合特定逻辑。我只需要 XML 文档说明 Lines 属性的“OrderLine 集合(超链接)”。

有没有一种简单的方法可以做到这一点?

【问题讨论】:

    标签: c# asp.net asp.net-web-api


    【解决方案1】:

    在您的 Web API 项目中,打开负责生成帮助页面模型类型名称的类的代码文件:

    {MyProject}/Areas/HelpPage/ModelDescriptions/ModelDescriptionGenerator.cs

    转到方法:

    public ModelDescription GetOrCreateModelDescription(Type modelType)
    

    然后在方法里面去下面的代码:

    if (modelType.IsEnum)
    {
        return GenerateEnumTypeModelDescription(modelType);
    }
    

    然后在紧随其后键入以下代码:

    // Force Web API to generate help page model type names like "Collection of {MyType}" instead of "Collection of Object".
    if (!modelType.IsGenericType                                // Model type must not be a generic type.
        && modelType.BaseType != null                           // Model type must have a base type (i.e. the model type is a derived type).
        && modelType.BaseType.IsGenericType                     // Model type base type must be a generic type.
        && typeof(IList).IsAssignableFrom(modelType.BaseType))  // Model type base type must implement the IList interface (you can replace "IList" with "IEnumerable" to handle more general collection types).
    {
        // Set the model type variable to the model type base type and the rest of the method will know what to do.
        modelType = modelType.BaseType;
    
    }
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多