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

时间:2023-03-09 00:01:36
亲手搭建一个基于Asp.Net WebApi的项目基础框架3

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

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

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

在website中引用dll亲手搭建一个基于Asp.Net WebApi的项目基础框架3

接下来我们就可以使用封装好的工具累了,如下所示亲手搭建一个基于Asp.Net WebApi的项目基础框架3,但是发现一个问题我们高兴的太早,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 = , [Description("服务方法异常,错误号:{0}")]
ServiceException = , [Description("Api参数错误,{0}")]
ApiParamError = , [Description("Json对象反序列化失败")]
JsonDeserializeFailed = , [Description("验证签名失败")]
ValidateSignFailed = , [Description("网络连接失败")]
HttpError =
} }

亲手搭建一个基于Asp.Net WebApi的项目基础框架3 现在我们把model 的dll取出来 也可以使用了

现在我们就可以完整地使用ServiceHandler这个工具类了亲手搭建一个基于Asp.Net WebApi的项目基础框架3