Show / Hide Table of Contents

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.

  • Paginate()
  • Paginate(String, Int32)
  • Paginate(String, String)
  • Paginate(String, String, Int32)

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.

C#
app.UseSeltzr<MyModel>(api => {
	api
		.Paginate()
		.CanGet();
});

The following example requests show the behavior of this method:

Request
GET https://localhost:5001/
Response
{
	"Elements": [
		...
	],
	"Count": 250
}
Request
GET https://localhost:5001/?page=2&count=50
Response
{
	"Elements": [
		...
	],
	"CurrentPage": 2,
	"TotalPages": 5,
	"Count": 50
}

Additionally, the overload method Paginate(String, String) takes in custom parameter names.

C#
app.UseSeltzr<MyModel>(api => {
	api
		.Paginate("pageNumber", "max")
		.CanGet();
});

To set a maximum page size limit, use the Paginate(String, String, Int32) overload.

C#
app.UseSeltzr<MyModel>(api => {
	api
		.Paginate("page", "count", 50)
		.CanGet();
});
Request
GET https://localhost:5001/?count=60
Response
{
	"Elements": [
		...
	],
	"CurrentPage": 1,
	"TotalPages": 5,
	"Count": 50
}

To prevent custom page sizes, use the Paginate(String, Int32) overload.

C#
app.UseSeltzr<MyModel>(api => {
	api
		.Paginate("page", 50)
		.CanGet();
});

The count parameter is now ignored:

Request
GET https://localhost:5001/?count=25
Response
{
	"Elements": [
		...
	],
	"CurrentPage": 1,
	"TotalPages": 5,
	"Count": 50
}
  • Improve this Doc
Back to top Generated by DocFX