• 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; }

    最后结果:

      希望能对大家有帮助!

  • 相关阅读:
    DDK 的一些笔记
    C# 32位程序访问64位系统注册表
    自己对设备栈的理解
    简单驱动编写与windbg调试
    DDK 的一些笔记other
    USB设备的一些概念
    C# 32位程序与64位程序读\写注册表的区别
    dbca建库时找不到ASM磁盘
    sf01_什么是数据结构
    cPickle.dump函数
  • 原文地址:https://www.cnblogs.com/Jaryleely/p/webapitest.html
Copyright © 2020-2023  润新知