I am trying to create a query string, which allows all the results to be filtered using pagination. (i.e. api/test?name=frs&page=2)
for example:
if the query returns 20 records of data, i would like the each page to show 10 lines of records, hence if I call [api/test?name=frs&page=1], and it would show me first 10 records of the query.
if I query [api/test?name=frs&page=2], then the page would now show me second set of 10 records.
Currently, I have tried writing some lines of code for the page filter but I am still unable to get all the records from the querystring (i.e. api/test?name=frs&page=1). This query returns total number of 20 records and it only shows first 10 and when I add in the page filter --> api/test?name=frs&page=2, it still shows the first set of 10 records, instead of showing the second set of 10 records.
public HttpResponseMessage get([FromUri] Query query )
{
if (User.IsInRole("admin"))
{
int pageSize = 10;
int page = 0;
var totalCount = Data.Count();
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
var pageValue = Request.RequestUri.ParseQueryString().Get("page");
int currentPage;
if (!int.TryParse(pageValue, out currentPage))
{
currentPage = 0;
}
Data = Data.OrderByDescending(c => c.UploadDate);
var data = Data.Skip(pageSize * page).Take(pageSize).ToList();
if (!data.Any())
{
var message = string.Format("No data found");
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}
return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, totalPages, pageValue, data });
}
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Access Denied");
}
Please help.
I hope i have clarified the pagination issue clearly, if not, please do let me know. Thank you for your reply