我需要添加简单的页面编号,并将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);
}
}
我建议对您的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);
}