.NET CORE API 统一封装结果返回

 public class Result
    {
        public static int SUCCESS = 1;
        public static int FAILURE = -1;
        public static int OTHER = 2;      //其他,页面显示
        public static int FAILURE_AUTHC = -9999;

        public static String STR_PARAM_NOT_NULL = "传入参数不能为空";
        public static String STR_SUCCESS = "操作成功";
        public static String STR_FAIL = "操作失败";


        /**
         * 请求是否错误,默认false
         */
        public Boolean success = false;
        /**
         * 请求返回code
         */
        public int code {get;set;}
        /**
         * 请求返回描述
         */
        public String msg {get;set;}

        public Object obj {get;set;}

        public Result() { }

        public Result(int code, String msg)
        {
            this.code = code;
            this.msg = msg;
        }
        public Result(int code, String msg, Boolean success)
        {
            this.code = code;
            this.msg = msg;
            this.success = success;
        }

        public Boolean isSuccess()
        {
            return success;
        }

        public void setSuccess(Boolean success)
        {
            this.success = success;
        }

        public String getMsg()
        {
            return msg;
        }

        public void setMsg(String msg)
        {
            this.msg = msg;
        }

        public Object getObj()
        {
            return obj;
        }

        public void setObj(Object obj)
        {
            this.obj = obj;
        }

        public int getCode()
        {
            return code;
        }

        public void setCode(int code)
        {
            this.code = code;
        }
    }

第二步:

 public class ApiResultHelper
    {
        #region 返回值结构
        /// 
        /// ajax失败
        /// 
        /// 失败的消息
        /// 
        public static Result renderError(string msg)
        {
            Result result = new Result();
            result.setCode(Result.FAILURE);
            result.setMsg(msg);
            return result;
        }

        /// 
        /// ajax失败
        /// 
        /// 失败的消息
        ///失败的编码
        /// 
        public static Result renderError(string msg, int code)
        {
            Result result = new Result();
            result.setCode(code);
            result.setMsg(msg);
            return result;
        }
        /// 
        /// ajax失败
        ///  
        /// 
        public static Result renderError()
        {
            Result result = new Result();
            result.setCode(Result.FAILURE);
            result.setMsg(Result.STR_FAIL);
            return result;
        }

        public static Result renderError(object obj)
        {
            Result result = new Result();
            result.setCode(Result.FAILURE);
            result.setObj(obj);
            return result;
        }

        /// 
        /// ajax失败,参数为空或错误
        ///  
        /// 
        public static Result renderErrorParamNotNull()
        {
            Result result = new Result();
            result.setCode(Result.FAILURE);
            result.setMsg(Result.STR_PARAM_NOT_NULL);
            return result;
        }

        /// 
        /// ajax成功
        ///  
        /// 
        public static Result renderSuccess()
        {
            Result result = new Result();
            result.setSuccess(true);
            result.setMsg(Result.STR_SUCCESS);
            return result;
        }

        /// 
        /// 成功
        /// 
        /// 消息
        /// 
        public static Result renderSuccess(string msg)
        {
            Result result = new Result();
            result.setSuccess(true);
            result.setMsg(msg);
            return result;
        }

        /// 
        /// 成功
        /// 
        /// 对象
        /// 
        public static Result renderSuccess(object obj)
        {
            Result result = new Result();
            result.setSuccess(true);
            result.setObj(obj);
            return result;
        }

        /// 
        /// 成功
        /// 
        /// 对象
        /// 消息
        /// 
        public static Result renderSuccess(object obj, string msg)
        {
            Result result = new Result();
            result.setSuccess(true);
            result.setMsg(msg);
            result.setObj(obj);
            return result;
        }
        #endregion

    }

第三步:调用

  [HttpGet]
        [Route("???")]
        public ActionResult GuestLogin()
        {
            var email = Appsettings.GetNode("email");
            var pass = Appsettings.GetNode("pass");
            try
            {
                var jm = ToolClass.AesEncrypt(pass, Appsettings.GetNode("??"), Appsettings.GetNode("??"));
                Dictionary dic = new Dictionary();
                dic.Add("pass", jm);
                if (!string.IsNullOrEmpty(email))
                {
                    dic.Add("email", email);
                }
                var res = HttpGetPost.DoPost(Appsettings.GetNode("??"), dic);
                return new ContentResult { Content = res.Result, ContentType = "application/json" };
            }
            catch (Exception)
            {
                return ApiResultHelper.renderError("Internal exception", 400);
            }

        }

你可能感兴趣的:(Asp.Net,Core,c#,mvc,.netcore,开发语言)