Reputation: 33
Is there any way to handle page size from the query parameter sent by the client. And not as default in the code as Enablequery(Pagesize = 100)
Upvotes: 1
Views: 776
Reputation: 27793
Is there any way to handle page size from the query parameter sent by the client. And not as default in the code as Enablequery(Pagesize = 100)
To achieve above requirement, you can try to create and use a customized EnableQueryAttribute, like below:
In customized EnableQueryAttribute
public class MyCustomQueryableAttribute : EnableQueryAttribute
{
public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
{
// dynamically set PageSize of ODataQuerySettings
// based on pagesize that client sent through querystring
StringValues ps;
// set default value to pagesize
int pagesize = 2;
if (queryOptions.Request.Query.TryGetValue("pagesize", out ps))
{
pagesize = int.Parse(ps);
}
var result = queryOptions.ApplyTo(queryable, new ODataQuerySettings { PageSize = pagesize });
return result;
}
}
In ODataController action
[MyCustomQueryable]
public IActionResult Get()
{
return Ok(_db.Books);
}
Test Result
Upvotes: 1