zoukankan      html  css  js  c++  java
  • NetCore3.1 自定义响应头

    一、自定义响应模型ApiResponse<T>

        /// <summary>
        /// 封装接口响应模型
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        public class ApiResponse<T>
        {
            public ApiResponse(T data )
            {
                Data = data;
            }
            public T Data { get; set; }
    
            public Metadata  Meta { get; set; }
        }

    二、自定义元数据Metadata

        /// <summary>
        /// 自定义元数据
        /// </summary>
        public class Metadata
        {
            public int TotalCount { get; set; }
            public int PageSize { get; set; }
            public int CurrentPage { get; set; }
            public int TotalPages { get; set; }
            public int? NextPageNumber { get; set; }
            public int? PreviousPageNumber { get; set; }
            public bool HasNextPage { get; set; }
            public bool HasPreviousPage { get; set; }
            public string NextPageUrl { get; set; }
            public string PreviousPageUrl { get; set; }
        }

    三、控制器中调用

     public IActionResult GetPosts([FromQuery] PostQueryFilter filters)
            {
                var posts = _postService.GetPosts(filters);
                var postDtos = _mapper.Map<IEnumerable<PostDto>>(posts);
    
    
                var metadata = new Metadata
                {
                    TotalCount = posts.TotalCount,
                    PageSize = posts.PageSize,
                    CurrentPage = posts.CurrentPage,
                    TotalPages = posts.TotalPages,
                    NextPageNumber = posts.NextPageNumber,
                    PreviousPageNumber = posts.PreviousPageNumber,
                    HasNextPage = posts.HasNextPage,
                    HasPreviousPage = posts.HasPreviousPage,
                    NextPageUrl = _uriService.GetPostPaginationUri(filters,Url.RouteUrl(nameof(GetPosts))).ToString(),
                    PreviousPageUrl = _uriService.GetPostPaginationUri(filters, Url.RouteUrl(nameof(GetPosts))).ToString()
    
                };

    四、效果展示

  • 相关阅读:
    webpack压缩图片之项目资源优化
    vue v-cloak 指令 处理页面显示源码
    js 获取url 参数
    element-ui Drawer抽屉组件封装
    js中的this指向
    对js闭包的理解
    vue作用域插槽
    flex布局实战
    vue 组件之间传值
    js 面试题一
  • 原文地址:https://www.cnblogs.com/ABC-wangyuhan/p/14869009.html
Copyright © 2011-2022 走看看