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()
    
                };

    四、效果展示

  • 相关阅读:
    Activity详解
    Log和LogCat的使用
    Android Studio项目目录结构
    Android系统架构
    [SCOI2016]美味
    [SCOI2016]背单词
    [SCOI2016]幸运数字
    [BZOJ4170]极光
    [JSOI2016]扭动的回文串
    [SCOI2016]萌萌哒
  • 原文地址:https://www.cnblogs.com/ABC-wangyuhan/p/14869009.html
Copyright © 2011-2022 走看看