NetCore3.1 自定义响应头
Posted ABC=>Go
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 自定义响应头的主要内容,如果未能解决你的问题,请参考以下文章