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

            };

四、效果展示

NetCore3.1 自定义响应头

 

 

NetCore3.1 自定义响应头

上一篇:4月数据库流行度排行出炉:MySQL 成事实王者


下一篇:BMZCTF WEB_ezeval