Pagination
The SeltzrOptionsBuilder<TModel, TUser> class includes a number of Paginate methods which can be used to easily add pagination to an API route.
Tip
The Paginate methods will automatically set the response values marked with CurrentPageAttribute and TotalPagesAttribute if the response is wrapped. Wrapping Responses is a good idea with pagination so that API consumers can navigate between pages more easily. The examples on this page will all implicitly use wrapped responses.
Usage
The following examples show typical usage of the Paginate methods.
Without any parameters, the Paginate() method will return all models with no parameters, or, if the count query parameter is specified, count number of models. Which models to show is determined by the page query parameter, which starts at 1.
app.UseSeltzr<MyModel>(api => {
api
.Paginate()
.CanGet();
});
The following example requests show the behavior of this method:
GET https://localhost:5001/
{
"Elements": [
...
],
"Count": 250
}
GET https://localhost:5001/?page=2&count=50
{
"Elements": [
...
],
"CurrentPage": 2,
"TotalPages": 5,
"Count": 50
}
Additionally, the overload method Paginate(String, String) takes in custom parameter names.
app.UseSeltzr<MyModel>(api => {
api
.Paginate("pageNumber", "max")
.CanGet();
});
To set a maximum page size limit, use the Paginate(String, String, Int32) overload.
app.UseSeltzr<MyModel>(api => {
api
.Paginate("page", "count", 50)
.CanGet();
});
GET https://localhost:5001/?count=60
{
"Elements": [
...
],
"CurrentPage": 1,
"TotalPages": 5,
"Count": 50
}
To prevent custom page sizes, use the Paginate(String, Int32) overload.
app.UseSeltzr<MyModel>(api => {
api
.Paginate("page", 50)
.CanGet();
});
The count parameter is now ignored:
GET https://localhost:5001/?count=25
{
"Elements": [
...
],
"CurrentPage": 1,
"TotalPages": 5,
"Count": 50
}