.NET WebApi 自定义返回类

77 阅读2分钟

一、背景

转c#开发了,恰好之前收集了一些Java模板类,现在使用c#改写,

原模板地址: Java SpringBoot模板 - 掘金 (juejin.cn)

二、改写结果响应类

2.1 创建枚举类

    public class AppHttpCodeEnum
    {
        // 成功段
        public static AppHttpCodeInfo SUCCESS = new(0, "操作成功");
        // 登录段1~50
        public static AppHttpCodeInfo NEED_LOGIN = new(1, "需要登录后操作");
        public static AppHttpCodeInfo LOGIN_PASSWORD_ERROR = new(2, "密码错误");
    }

    public class AppHttpCodeInfo
    {
         private int Code;
         private string ErrorMessage;

        public AppHttpCodeInfo(int code, string errorMessage)
        {
            this.Code = code;
            this.ErrorMessage = errorMessage;
        }

        public int getCode()
        {
            return Code;
        }

        public String getErrorMessage()
        {
            return ErrorMessage;
        }
    }

2.2 创建结果类

    public class ResponseResult
    {
        public int Code { get; set; }
        public string ErrorMessage { get; set; }
        public object Data { get; set; }

        public ResponseResult()
        {
            this.Code = 200;
        }

        public ResponseResult(int code, object data)
        {
            this.Code = code;
            this.Data = data;
        }

        public ResponseResult(int code, String msg, object data)
        {
            this.Code = code;
            this.ErrorMessage = msg;
            this.Data = data;
        }

        public ResponseResult(int code, String msg)
        {
            this.Code = code;
            this.ErrorMessage = msg;
        }

        public ResponseResult error(int code, String msg)
        {
            this.Code = code;
            this.ErrorMessage = msg;
            return this;
        }

        public ResponseResult ok(object data)
        {
            this.Data = data;
            return this;
        }

        public ResponseResult ok(int code, object data)
        {
            this.Code = code;
            this.Data = data;
            return this;
        }

        public ResponseResult ok(int code, object data, String msg)
        {
            this.Code = code;
            this.Data = data;
            this.ErrorMessage = msg;
            return this;
        }

   

        public static ResponseResult setAppHttpCodeEnum(AppHttpCodeInfo info)
        {
            return okResult(info.getCode(), info.getErrorMessage());
        }

        public static ResponseResult setAppHttpCodeEnum(AppHttpCodeInfo enums, string errorMessage)
        {
            return okResult(enums.getCode(), errorMessage);
        }


        public static ResponseResult okResult(int code, string msg)
        {
            ResponseResult result = new ResponseResult();
            return result.ok(code, null, msg);
        }

        public static ResponseResult okResult(Object data)
        {

            ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
            if (data != null)
            {
                result.Data = data;
            }
            return result;
        }

        public static ResponseResult errorResult(int code, String msg)
        {
            ResponseResult result = new ResponseResult();
            return result.error(code, msg);
        }

        public static ResponseResult errorResult(AppHttpCodeInfo enums)
        {
            ResponseResult result = new ResponseResult();
            
            return result.error(enums.getCode(),enums.getErrorMessage());
        }

    }

三、使用

[ApiController]
[Route("[controller]/[action]")]
public class WeatherForecastController : ControllerBase
{

    [HttpGet]
    public ResponseResult OK()
    {
        return ResponseResult.okResult(new Student
        {
            Id = 1,
            Name = "zhangsan"
        }) ;
    }

    [HttpGet]
    public ResponseResult fail1()
    {
        // return ResponseResult<object>.errorResult(123,"tst");
        return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
    }

    [HttpGet]
    public ResponseResult fail2()
    {
        return ResponseResult.errorResult(123,"tst");
    }
}

3.1 成功返回

{
  "code": 0,
  "errorMessage": "操作成功",
  "data": {
    "id": 1,
    "name": "zhangsan"
  }
}

3.2 失败——根据模板类返回

{
  "code": 2,
  "errorMessage": "密码错误",
  "data": null
}

3.3 失败——自定义返回信息和状态码

{
  "code": 123,
  "errorMessage": "tst",
  "data": null
}

四、总结

当响应成功时,调用ResponseResult.okResult(xxxx),将数据传入okResult中即可;

当响应失败时,调用ResponseResult.errorResult(xxxx), 可从枚举类中选择或新建错误信息。

前端接收后,根据状态码分别处理。