.Net Core webapi RestFul 统一接口数据返回格式

news/2024/5/19 21:48:39 标签: .netcore, restful, webapi, c#

在RestFul风格盛行的年代,大部分接口都需要一套统一的数据返回格式,那么我们怎么才能保证使用统一的json数据格式返回呢,下面给大家简单介绍一下:

假如我们需要接口统一返回一下数据格式:

{
  "statusCode": 200,
  "successful": true,
  "message": null,
  "data": object
}

json对象中data是返回的实际结果对象,可以是一个对象、一个list、一个字符串、一个数字......

但是整体的json格式要以上面的为准

一般这种情况我们有两种实现方式:

1、自定义一个统一返回类

2、使用过滤器(Filter)实现

下面先介绍第一种,自定义一个接口返回类:

创建对应的接口类和实现类

  • ApiResponse.cs//实现类
  • IApiResponse.cs://几个相关的接口

IApiResponse.cs

    public interface IApiResponse
    {
        public int StatusCode { get; set; }
        public bool Successful { get; set; }
        public string? Message { get; set; }
    }

    public interface IApiResponse<T> : IApiResponse
    {
        public T? Data { get; set; }
    }

    public interface IApiErrorResponse
    {
        public Dictionary<string, object> ErrorData { get; set; }
    }

保证了所有相关对象都来自 IApiResponse 接口

ApiResponse.cs

    public class ApiResponse<T> : IApiResponse<T>
    {
        public ApiResponse()
        {
        }

        public ApiResponse(T? data)
        {
            Data = data;
        }

        public int StatusCode { get; set; } = 200;
        public bool Successful { get; set; } = true;
        public string? Message { get; set; }

        public T? Data { get; set; }

        /// <summary>
        /// 实现将 <see cref="ApiResponse"/> 隐式转换为 <see cref="ApiResponse{T}"/>
        /// </summary>
        /// <param name="apiResponse"><see cref="ApiResponse"/></param>
        public static implicit operator ApiResponse<T>(ApiResponse apiResponse)
        {
            return new ApiResponse<T>
            {
                StatusCode = apiResponse.StatusCode,
                Successful = apiResponse.Successful,
                Message = apiResponse.Message
            };
        }
    }


    public class ApiResponse : IApiResponse, IApiErrorResponse
    {
        public int StatusCode { get; set; } = 200;
        public bool Successful { get; set; } = true;
        public string? Message { get; set; }
        public object? Data { get; set; }

        /// <summary>
        /// 可序列化的错误
        /// <para>用于保存模型验证失败的错误信息</para>
        /// </summary>
        public Dictionary<string, object>? ErrorData { get; set; }

        public ApiResponse()
        {
        }

        public ApiResponse(object data)
        {
            Data = data;
        }

        public static ApiResponse NoContent(string message = "NoContent")
        {
            return new ApiResponse
            {
                StatusCode = StatusCodes.Status204NoContent,
                Successful = true,
                Message = message
            };
        }

        public static ApiResponse Ok(string message = "Ok")
        {
            return new ApiResponse
            {
                StatusCode = StatusCodes.Status200OK,
                Successful = true,
                Message = message
            };
        }

        public static ApiResponse Ok(object data, string message = "Ok")
        {
            return new ApiResponse
            {
                StatusCode = StatusCodes.Status200OK,
                Successful = true,
                Message = message,
                Data = data
            };
        }

        public static ApiResponse Unauthorized(string message = "Unauthorized")
        {
            return new ApiResponse
            {
                StatusCode = StatusCodes.Status401Unauthorized,
                Successful = false,
                Message = message
            };
        }

        public static ApiResponse NotFound(string message = "NotFound")
        {
            return new ApiResponse
            {
                StatusCode = StatusCodes.Status404NotFound,
                Successful = false,
                Message = message
            };
        }

        public static ApiResponse BadRequest(string message = "BadRequest")
        {
            return new ApiResponse
            {
                StatusCode = StatusCodes.Status400BadRequest,
                Successful = false,
                Message = message
            };
        }

        public static ApiResponse BadRequest(ModelStateDictionary modelState, string message = "ModelState is not valid.")
        {
            return new ApiResponse
            {
                StatusCode = StatusCodes.Status400BadRequest,
                Successful = false,
                Message = message,
                ErrorData = new SerializableError(modelState)
            };
        }

        public static ApiResponse Error(string message = "Error", Exception? exception = null)
        {
            object? data = null;
            if (exception != null)
            {
                data = new
                {
                    exception.Message,
                    exception.Data
                };
            }

            return new ApiResponse
            {
                StatusCode = StatusCodes.Status500InternalServerError,
                Successful = false,
                Message = message,
                Data = data
            };
        }
    }

分别实现类带有泛型的 ApiResponse<T> 类和 ApiResponse 类 

注意在泛型类中,使用运算符重载,实现了 ApiResponse 到 ApiResponse<T> 的隐式转换。

在接口实现处返回一个 ApiResponse<T> 对象:

        [HttpGet]
        public ApiResponse<UserEntity> Get(string name)
        {
            var list = new List<UserEntity>() 
            {
                new UserEntity() { Name = "张三" },
                new UserEntity(){Name = "李四"}
            };
            var user = list.Find(p => p.Name == name);
            return user == null ? ApiResponse.NotFound() : new ApiResponse<UserEntity>(user);
        }

注意看最后一句 

return user == null ? ApiResponse.NotFound() : new ApiResponse<UserEntity>(user);

ApiResponse.NotFound() 返回的是一个 ApiResponse 对象

但这接口的返回值明明是 ApiResponse<UserEntity> 类型呀,这不是类型不一致吗?

不过在 ApiResponse<T> 中,我们定义了一个运算符重载,实现了 ApiResponse 类型到 ApiResponse<T> 的隐式转换,所以就完美解决这个问题,大大减少了代码量。

来看一下最后运行效果:

下一章介绍一下如何通过自定义过滤器的方式实现统一接口返回数据格式:

.NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式-CSDN博客


http://www.niftyadmin.cn/n/5278702.html

相关文章

Http---URL

1. URL的概念 URL的英文全拼是(Uniform Resoure Locator),表达的意思是统一资源定位符&#xff0c;通俗理解就是网络资源地址&#xff0c;也就是我们常说的网址。 2. URL的组成 URL的样子: https://www.163.com/dy/article/HLRBVSRO0516C53P.html URL的组成部分: 协议部分…

docker中nginx添加SSL证书,并支持http和https连接

背景 最近在弄一个小程序&#xff0c;小程序上线需要后端https访问。那我原先的需要在nginx中添加SSL证书 关于SSL证书&#xff0c;可以去阿里云或者腾讯云去申请。不过现在个人免费版只有3个月的有效期。我们申请到证书后&#xff0c;放到nginx中 我们新增了一个cert文件夹 m…

在 Go 语言中使用 regexp 包处理正则表达式

Go 语言&#xff0c;作为一门现代的编程语言&#xff0c;提供了对正则表达式的强大支持。在 Go 中&#xff0c;正则表达式的功能通过标准库中的 regexp 包来实现。本文将介绍如何在 Go 中使用 regexp 包来编译和执行正则表达式&#xff0c;以及如何从文本中匹配和提取信息。 正…

基于[Discretized] Torus的全同态加密指引(2)

前序博客有&#xff1a; 基于[Discretized] Torus的全同态加密指引&#xff08;1&#xff09; 5. 基于已加密数据处理 很显然&#xff0c;TLWE加密方案和TGLWE加密方案均具有加法同态性。[GSW13] Gentry–Sahai–Waters 方法使用matrix product来将TLWE加密方案和TGLWE加密方…

融资项目——vue之双向数据绑定

上一篇文章中使用的v-bind是单向绑定方法&#xff0c;即数据改变&#xff0c;网页相应的视图发生改变&#xff0c;但是网页视图发生改变其相关联的数据不会发生改变。但是双向数据绑定不同之处在于网页视图发生改变其相关联的数据也会发生改变。Vue可以使用v-model进行双向数据…

从安全、开发、产品三个角度反对用refresh_token续期access_token的观点

说明&#xff1a; access_token&#xff1a; 服务端与客户端通信&#xff0c;有时服务端需要知道客户端的身份&#xff0c;就会用到access_token来用于验证身份。 refresh_token&#xff1a; 但为了保证安全token会设置过期时间&#xff0c;如果直接过期&#xff0c;相当于用户…

Go 语言实战:掌握正则表达式的应用与技巧

Go 语言实战&#xff1a;掌握正则表达式的应用与技巧 1. 引言2. 正则表达式基础2.1 基本概念2.2 常见元素2.3 基本示例 3. Go语言中的正则表达式库3.1 引入regexp包3.2 编译正则表达式3.3 使用正则表达式3.4 示例代码 4. 常用正则表达式函数及使用示例4.1 MatchString4.2 FindS…

国家高新技术企业认定,如何提前准备?

国家高新技术企业认定是企业发展的重要里程碑&#xff0c;它不仅代表了企业的技术实力&#xff0c;还能带来一系列的政策优惠和支持。然而&#xff0c;高新技术企业认定有着严格的条件和流程&#xff0c;需要企业提前做好充分的准备。本文将为您详细解答如何提前准备国家高新技…