Paging in .NET Web API

949 views Asked by At

I have to do paging for an odata endpoint built using Entity Framework . I know I can do it using

private ODataQuerySettings settings = new ODataQuerySettings();
settings.PageSize = myPageSize; // I keep this value in web.config of solution
and
options.ApplyTo(IQueryable, settings);

But I am constrained not to use ApplyTo (i.e. I don't want to use the settings above) and take the page size from the web.config of my solution without modifying the url presented by the web api i.e. no client size paging.

So, far I haven't found a way to do this. I can't put page size in [ Enable Query ] as that is not dynamically lifting page size parameter from web.config.

I wonder if what I want could be done or am I trying to do something too tricky.

1

There are 1 answers

0
Daniel Oliveira On BEST ANSWER

You can extend the default behavior of the EnableQuery attribute to use web.config's value as you want. Maybe something like this:

public class EnablePagedQueryAttribute : EnableQueryAttribute
{
    public EnablePagedQueryAttribute()
    {
        int myPageSizeFromWebConfig = 0;

        // Get value from web.config as you want:
        if (int.TryParse(ConfigurationManager.AppSettings["myPageSize"], out myPageSizeFromWebConfig))
        {
            this.PageSize = myPageSizeFromWebConfig;
        }
    }
}