How to create page filter parameter in web api?
ASP.NET
1
Posts
1
Posters
0
Views
1
Watching
-
I am writing to seek help in, how can I add page filter parameter to my method below, which can allow me to call a query string such as -- api/feed?name=st&page=2. I have tried using the page size property to make this work but the output would always give me 'no data response'. This is what I currently have and I am little stuck in how can I make this work. Please advice. Any solution would be most helpful. Many thanks.
public HttpResponseMessage get([FromUri] Query query )
{
int pageSize = 10;
int page = 0;IQueryable Data = null; if (!string.IsNullOrEmpty(query.name)) { var ids = query.name.Split(','); var dataMatchingTags = db.data\_qy.Where(c => ids.Any(id => c.Name.Contains(id))); if (Data == null) Data = dataMatchingTags; else Data = Data.Union(dataMatchingTags); } if (Data == null) Data = db.data\_qy; if (query.endDate != null) { Data = Data.Where(c => c.UploadDate <= query.endDate); } if (query.startDate != null) { Data = Data.Where(c => c.UploadDate >= query.startDate); } var totalCount = Data.Count(); // var totalPages = (int)Math.Ceiling((double)totalCount / pageSize); // var urlHelper = new UrlHelper(Request); // var prevLink = page > 0 ? urlHelper.Link("DefaultApi", new { page = page - 1 }) : ""; // var nextLink = page < totalPages - 1 ? urlHelper.Link("DefaultApi", new { page = page + 1 }) : ""; 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, data); //return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, totalPages, prevLink, nextLink, data }); return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, data }); }