I'm currently working with the web api in .NET Core 3.1 and I'm working with many GET requests that works with pagination (the client sends a request to get items, provides the page number, amount per items, etc, then receives the items).
I noticed that when I want to use this pagination option, it's creating sort of code duplication (every time I need to put this specific code: [FromQuery] page, [FromQuery] perPage
).
I was wondering if there is a way to define those parameter in one specific option and then use it wherever I need.
That way I will prevent code duplication and it will be much easier if I'll ever make changes to the pagination system.
EDIT: the page
and perPage
parameters are int
s.
CodePudding user response:
Define a class with all property needs for pagination
public class QueryParam
{
public int Page {get; set;}
public int PerPage {get; set;}
}
Then use that class in controller's methods
public async Task<IActionResult> GetSomething([FromQuery] QueryParam query)
{
return Ok();
}