VS为WebAPI生成了帮助页,属性文档
本文关键字:属性 文档 帮助 WebAPI VS | 更新日期: 2023-09-27 17:58:27
我正在为Web API应用程序使用Visual Studio 2013自动生成的XML文档,它运行良好,但继承自List<>的业务对象的属性除外;我从他们那里得到的只是"对象集合"
例如,这里有一个Order对象,它包含OrderLineCollection属性:
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列中只有"Collection of object",没有指向OrderLine对象的链接。
如果我这样定义Lines属性,它就会起作用(=我在Type列中得到"Collection of OrderLine",其中OrderLine是超链接的):
public class Order
{
public List<OrderLine> Lines { get; set; }
}
但是,我希望能够使用如上所述的OrderLineCollection类,这样我就可以在那里保留集合特定的逻辑。我只需要XML文档为Lines属性显示"CollectionofOrderLine(hyperlinked)"。
有没有一种简单的方法可以实现这一点?
在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;
}
我希望这能有所帮助!