我正在写信寻求帮助,如何在下面的web api方法中实现页码过滤器。我知道'pagesize'和'take'属性,但这只允许我过滤记录数。
我希望能够显示所有记录但是通过api / test等页面传播数据?name = frs& page = 2
请提供一些帮助/指导,了解如何进一步开始执行此任务,谢谢。
public HttpResponseMessage get([FromUri] Query query )
{
int pageSize = 10;
int page = 0;
IQueryable<data_qy> Data = null;
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 });
}
答案 0 :(得分:1)
尝试将此与您的分页代码结合使用:
var pageValue = Request.RequestUri.ParseQueryString().Get("page");
int currentPage;
if (!int.TryParse(pageValue, out currentPage))
{
currentPage = 0;
}