1

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)

2
  • Does this answer your question? Implement Pagination in ASP.NET Core 2.1 Web API Commented 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. Commented Dec 30, 2019 at 2:10

1 Answer 1

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.