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)
- Does this answer your question? Implement Pagination in ASP.NET Core 2.1 Web APIManish– Manish2019-12-29 15:50:31 +00:00Commented Dec 29, 2019 at 15:50
- Not really what i need is how to set the pagesize from a query parameter if it is sent by the client or apply a default if nothing is sent by client. Because in odata documentation they show only default pagesize. The purpose is to generate the pagination link from odata.Nasreddine– Nasreddine2019-12-30 02:10:53 +00:00Commented Dec 30, 2019 at 2:10
Add a comment |
1 Answer
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
