亲手搭建一个基于Asp.Net WebApi的项目基础框架3

 1:使用Framework工具类封装http请求

     接上第二篇的步骤,现在在站点中使用封装好的组件,将framework编译好之后把dll提取出来,然后放到lib当中

       

   在website中引用dll

    接下来我们就可以使用封装好的工具累了,如下所示,但是发现一个问题我们高兴的太早,Request方法需要一个泛型参数去接收返回值,这里我们其实可以根据实际需要随时随便写一个类型附上去,但是为了标准化统一返回和请求的过程,我决定在封装一个Response<T>,这样我们返回的格式都是一样的,在其它接口上都可以使用。我们在Server项目的Model里面去建立这样一个对象。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zjl.Model.ResponseBody
{
    public class ResponseBase
    {
        public ResultTypeEnum ResultType { get; set; }

        public string Message { get; set; }
    }

    public class Response<T> : ResponseBase
    {

        public T Data { get; set; }

        public Response()
        {
        }

        public Response(T data)
        {
            this.ResultType = ResultTypeEnum.Success;
            this.Data = data;
        }


        public Response(T data, string strMsg)
        {
            this.ResultType = ResultTypeEnum.Success;
            this.Data = data;
            this.Message = strMsg;
        }

        public Response(ResultTypeEnum resultType, string message)
        {
            ResultType = resultType;
            Message = message;
        }

    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zjl.Model.ResponseBody
{
        public enum ResultTypeEnum
        {
            [Description("成功")]
            Success = 1,

            [Description("服务方法异常,错误号:{0}")]
            ServiceException = 1000,

            [Description("Api参数错误,{0}")]
            ApiParamError = 1001,

            [Description("Json对象反序列化失败")]
            JsonDeserializeFailed = 1002,

            [Description("验证签名失败")]
            ValidateSignFailed = 1003,

            [Description("网络连接失败")]
            HttpError = 1004
        }

}

 现在我们把model 的dll取出来 也可以使用了

现在我们就可以完整地使用ServiceHandler这个工具类了

原文地址:https://www.cnblogs.com/zzlblog/p/8528855.html