简单/基本页面编号和.NET Core 2.2 Web API中限制返回数据的大小

问题描述 投票:0回答:1

我需要添加简单的页面编号,并将Web API中返回的数据的大小限制为此类控制器。整个项目都在GitHub上,好像有人想看到更多。

https://github.com/lukaszFD/IPI-PAN_WEB_API

public class Documentation
{
        private readonly DocumentationContext _doc;
        public Documentation()
        {
            _doc = new DocumentationContext();
        }
        public async Task<List<GrTables>> DBDocumentation()
        {
            List<GrTables> list = await Task.Run(() => _doc.GrTables.ToList()).ConfigureAwait(true);
            return list;
        }
}
public class GrTablesController : ControllerBase
{
        private readonly ILogger<GrTablesController> _logger;

        public GrTablesController(ILogger<GrTablesController> logger)
        {
            _logger = logger;
        }
        [HttpGet("Documentation/All")]
        public async Task<ActionResult<List<GrTables>>> GetGlobalRespositoryDocumentation()
        {
            var data = await new Documentation().DBDocumentation();

            if (data == null)
            {
                _logger.LogWarning("No data found for GetGlobalRespositoryDocumentation/Documentation/All");
                return NotFound();
            }
            return Ok(data);
        }
}
c# .net .net-core asp.net-core-2.2
1个回答
0
投票

我建议对您的EF查询使用分页。像这样的东西:

  public async Task<List<GrTables>> GetPaginatedResult(int pageSize, int pageNumber)

  {
            var query = _doc.Set<GrTables>().AsQueryable();
            return await query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync();
  }

我还希望返回带有有助于处理和请求更多分页数据的信息的分页数据。

 public class PageMetadata
 {
        public int TotalRecordCount { get; set; }

        public string NextPageLink { get; set; }

        public string PreviousPageLink { get; set; }

        public string FirstPageLink { get; set; }

        public string LastPageLink { get; set; }

        public int ActualPage { get; set; }

        public int RecordsPerPage { get; set; }

        public int TotalPages { get; set; }
 }

 public class PagedResponse<TResponseModel>
 {
        public IEnumerable<TResponseModel> Records { get;  set; }

        public PageMetadata Metadata { get;  set; }
 }
[HttpGet]
public async Task<IActionResult> GetPagedServiceData(
            [FromQuery] int pageNumber,
            [FromQuery] int pageSize)
{
            var result  = await var data = await new Documentation().GetPaginatedResult(pageNumber, pageSize);
            return Ok(result);
}

© www.soinside.com 2019 - 2024. All rights reserved.