Pagination
MapCrud makes it easy to paginate the results of your GET requests, allowing clients to request data in manageable chunks. This is essential for performance and usability when dealing with large datasets.
Pagination is controlled by two query parameters:
page: Specifies the page number to retrieve. It defaults to1if not provided.pageSize: Specifies the number of items to include on each page. It defaults to20if not provided.
How to Use
When you map an entity using app.MapCrud<TEntity>(), pagination is automatically enabled for the endpoint that retrieves multiple entities (e.g., GET /your-entity-route).
Example Model
Consider the following Product model:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int StockQuantity { get; set; }
}
If you've mapped it like this:
app.MapCrud<Product>("/products");
Requesting Paginated Data
Clients can request specific pages of data by including the page and pageSize query parameters in their HTTP GET request.
Example 1: Get the first page with default page size (20 items)
GET /products
or explicitly:
GET /products?page=1&pageSize=20
Example 2: Get the third page of products, with 10 products per page
curl "http://localhost:5000/products?page=3&pageSize=10"
URL: http://localhost:5000/products?page=3&pageSize=10
Example 3: Get the first page with 5 products per page
curl "http://localhost:5000/products?pageSize=5"
URL: http://localhost:5000/products?pageSize=5 (page defaults to 1)
OpenAPI (Swagger) Behavior
When MapCrud generates OpenAPI documentation for your endpoints, the page and pageSize query parameters will be automatically included for the relevant GET endpoints.
This means that when you view your API documentation (e.g., via Swagger UI), you will see these parameters listed, along with their descriptions, allowing users to easily understand and test the pagination feature.
For the GET /products endpoint, the Swagger UI would show:
page(integer, query): The page number to retrieve. Defaults to 1.pageSize(integer, query): The number of items per page. Defaults to 20.
This provides clear guidance to API consumers on how to use pagination.