ASP.NET Core Web API Interview Question #46
How should an API return collection data?
DTOs, Model Binding, Validation & API Responses Junior Beginner
Quick Interview Answer
A collection endpoint should return a predictable array or collection representation and an appropriate success status. For large datasets, the endpoint should support pagination rather than returning the entire table.
Detailed Explanation
A collection endpoint should return a predictable array or collection representation and an appropriate success status. For large datasets, the endpoint should support pagination rather than returning the entire table.
Filtering, sorting, and paging inputs normally belong in the query string. Response metadata can be returned in a response envelope or headers according to the documented API contract.
Code Example
[HttpGet]
public async Task<ActionResult<IReadOnlyList<ProductResponse>>> GetAll(
[FromQuery] int page = 1,
[FromQuery] int pageSize = 20,
CancellationToken token = default)
{
var items = await _service.GetPageAsync(
page, pageSize, token);
return Ok(items);
}