zoukankan      html  css  js  c++  java
  • 亲手搭建一个基于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这个工具类了

  • 相关阅读:
    修改带!important的css样式
    解决Eclipse导入项目是提示错误:Some projects cannot be imported because they already exist in the workspace
    HTML5——canvas:使用画布绘制卡片
    vue:更改组件样式
    导入导出大量excel文件
    winfrom控件Treeview绑定数据库中的资料(节点控件)
    Winfrom将excel中的数据导入sqlserver数据库中的方法
    C# 将DataTable表中的数据批量插入到数据库表中的方法
    创建Sql数据表的sql代码
    Winfrom之SplitContainer控件
  • 原文地址:https://www.cnblogs.com/zzlblog/p/8528855.html
Copyright © 2011-2022 走看看