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

最后結果:

  希望能對大家有幫助!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM