WebApiTestClient自定义返回值说明

  WebApiTestClient是基于微软HelpPage一个客户端调试扩展工具,用来做接口调试比较方便。但是对返回值的自定义说明还是有缺陷的。有园友写过一篇文章,说可以通过对类进行注释,然后通过在IHttpActionResult上标记ResponseType(typeof(class))即可。

1
2
3
4
5
6
7
8
9
10
11
12
[ResponseType(typeof(CreditRuleDetails))]
 public IHttpActionResult GetCreditRuleList(int ruleType = 1)
 {
     try
     {
   
     }
     catch (Exception exception)
     {
 
     }
 }
1
CreditRuleDetails类
1
2
3
4
5
6
7
8
9
10
11
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; }

最后结果:

  希望能对大家有帮助!

posted @   Jaryleely  阅读(1833)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
历史上的今天:
2009-07-11 工作四个月之后的小感
TOP
学习,一直学习,就这样下去。。。
点击右上角即可分享
微信分享提示