WebApiTestClient自定义返回值说明
WebApiTestClient是基于微软HelpPage一个客户端调试扩展工具,用来做接口调试比较方便。但是对返回值的自定义说明还是有缺陷的。有园友写过一篇文章,说可以通过对类进行注释,然后通过在IHttpActionResult上标记ResponseType(typeof(class))即可。
[ResponseType(typeof(CreditRuleDetails))] public IHttpActionResult GetCreditRuleList(int ruleType = 1) { try { } catch (Exception exception) { } }
CreditRuleDetails类
public class CreditRuleDetails { /// <summary> /// 规则Id /// </summary> public int RuleId{get;set;} /// <summary> /// 规则名称 /// </summary> public int RuleName{get;set;} }
但发现返回值的Description中没有summary描述。
弄了半天,也没发现是什么问题,后来转变了下思路。改用C#特性来做,然后更改了下ModelDescriptionGenerator.cs的方法实现。
private ModelDescription GenerateComplexTypeModelDescription(Type modelType) { ComplexTypeModelDescription complexModelDescription = new ComplexTypeModelDescription { Name = ModelNameHelper.GetModelName(modelType), ModelType = modelType, Documentation = CreateDefaultDocumentation(modelType) }; GeneratedModels.Add(complexModelDescription.Name, complexModelDescription); bool hasDataContractAttribute = modelType.GetCustomAttribute<DataContractAttribute>() != null; PropertyInfo[] properties = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { if (ShouldDisplayMember(property, hasDataContractAttribute)) { ParameterDescription propertyModel = new ParameterDescription { Name = GetMemberName(property, hasDataContractAttribute) }; if (DocumentationProvider != null) { //======以下是添加的========= DescriptionAttribute myattribute = (DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute)); if (myattribute != null) { propertyModel.Documentation = myattribute.Description; } //========以上是添加的=========== else { propertyModel.Documentation = DocumentationProvider.GetDocumentation(property); } }
然后将类的属性标记为Description。
[Description("规则名称")] public string RuleName { get; set; }
最后结果:
希望能对大家有帮助!