如何在web api中创建分页? [英] How to create pagination in web api?
问题描述
我正在尝试创建一个查询字符串,它允许使用分页过滤所有结果。 (即api / test?name = frs& page = 2)
例如:
如果查询返回20条数据记录,我希望每个页面显示10行记录,因此如果我调用[api / test?name = frs& page = 1],它将显示查询的前10条记录。
如果我查询[api / test?name = frs& page = 2],那么页面现在会显示第二组10条记录。
目前,我已经尝试为页面过滤器编写一些代码行,但我仍然无法从查询字符串中获取所有记录(即api / test?name = frs& page = 1)。此查询返回总共20条记录,它只显示前10条,当我在页面过滤器中添加时 - > api / test?name = frs& page = 2,它仍然显示第一组10条记录,而不是显示第二组10条记录。
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;
//code for getting data (i.e. name, tag etc)
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 }) : "";
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, data);
// return Request.CreateResponse(HttpStatusCode.OK, new { totalCount, totalPages, prevLink, nextLink, data });
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
推荐答案
看起来你在skip方法中使用了错误的变量。页面变量始终设置为0,从查询字符串获得的页面变量在prasing后命名为currentPage。想你忘了你创建了第一个变量。
It looks like you using the wrong variable in your skip method. The page variable is always set to 0 and the one you get from your query string is named currentPage after prasing. Think you forgot you created the first variable.
var data = Data.Skip(pageSize * page).Take(pageSize).ToList();
修复:
Fix:
var data = Data.Skip(pageSize * currentPage).Take(pageSize).ToList();
您还可以在TryPrase方法中将输出参数更改为page。无论哪种方式都有效。
You could also change the output parameter to page in your TryPrase method. Either way works.
这篇关于如何在web api中创建分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!