[英]Easy/basic page numbering and limiting the size of returned data in the .NET Core 2.2 Web API
I need to add easy page numbering and limiting the size of returned data in Web API to such a controller.我需要添加简单的页码并将 Web API 中返回数据的大小限制到这样的控制器。 The whole project is on GitHub as if someone would like to see more.
整个项目都在 GitHub 上,好像有人想看更多。
https://github.com/lukaszFD/IPI-PAN_WEB_API 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);
}
}
I would recommend using pagination on Your EF queries.我建议在您的 EF 查询中使用分页。 Something like this:
像这样的东西:
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();
}
I also like to return the paginated data with info that helps to process and request more paginated data.我还喜欢返回带有有助于处理和请求更多分页数据的信息的分页数据。
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 new Documentation().GetPaginatedResult(pageNumber, pageSize);
return Ok(result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.