Server-side paging populates each client page by requesting only its relevant records from the web API, instead of downloading the complete dataset. section 5.4.1
Implement paging with three values:
- PageIndex: a zero-based page number passed by the client; the first page is 0. section 5.4.1
- PageSize: the number of records per page, supplied by the client or constrained by the server. section 5.4.1
- RecordCount: the server-calculated total, allowing the client to determine available pages or records. section 5.4.1
Listing 5.5 BoardGameController.Get() method (with paging)
[HttpGet(Name = "GetBoardGames")]
[ResponseCache(Location = ResponseCacheLocation.Any, Duration = 60)]
public async Task<RestDTO<BoardGame[]>> Get(
int pageIndex = 0,
int pageSize = 10)
{
var query = _context.BoardGames
.Skip(pageIndex * pageSize)
.Take(pageSize);
return new RestDTO<BoardGame[]>()
{
Data = await query.ToArrayAsync(),
PageIndex = pageIndex,
PageSize = pageSize,
RecordCount = await _context.BoardGames.CountAsync(),
Links = new List<LinkDTO> {
new LinkDTO(
Url.Action(
null,
"BoardGames",
new { pageIndex, pageSize },
Request.Scheme)!,
"self",
"GET"),
}
};
}
The BoardGames endpoint applies Skip(pageIndex * pageSize) and Take(pageSize), then returns the selected data with paging metadata and a self link. section 5.4.1
