我尝试使用ASP 5.0 WebAPI在Customer控制器中进行分页。当我尝试这个例子时,我得到:
无法隐式转换类型Microsoft.AspNet.Mvc.HttpOkObjectResult 到API.Controllers.IHttpActionResult
我只需要返回一组客户,其中包含当前页面的编号,页面总数和结果。
public IHttpActionResult Get(int offset = 0, int limit = 50)
{
// Get total number of records
int total = _dbContext.Customers.Count();
// Select the customers based on paging parameters
var customers = _dbContext.Customers
.OrderBy(c => c.Id)
.Skip(offset)
.Take(limit)
.ToList();
// Return the list of customers
return Ok(new
{
Data = customers,
Paging = new
{
Total = total,
Limit = limit,
Offset = offset,
Returned = customers.Count
}
});
}
答案 0 :(得分:2)
您需要将IHttpActionResult
更改为IActionResult
Controller.Ok()
返回HttpOkObjectResult ...
public class HttpOkObjectResult : ObjectResult, IActionResult {...}
...虽然您的方法签名定义了IHttpActionResult
,它来自之前的版本