zoukankan      html  css  js  c++  java
  • 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; }

    最后结果:

      希望能对大家有帮助!

  • 相关阅读:
    11.4 iftop:动态显示网络接口流量信息
    10.2-3 ifup&ifdown:激活与禁用网络接口
    10.1 ifconfig:配置或显示网络接口信息
    10.16-17 mailq&mail:显示邮件传输队列&发送邮件
    10.13 nc:多功能网络工具
    Matplotlib_常用图表
    urlrrtrieve()实例_下载微博短视频
    ulrlib案例-爬取百度贴吧
    Scrapy-Spider
    Scrapy-架构
  • 原文地址:https://www.cnblogs.com/Jaryleely/p/webapitest.html
Copyright © 2011-2022 走看看