c# - WebAPI One Asp.Net (ASP5/vNext) 中的分页

标签 c# asp.net linq asp.net-web-api

我正在尝试使用 ASP 5.0 WebAPI 在客户 Controller 中进行分页。当我尝试做这个例子时,我得到:

Cannot implicitly convert type Microsoft.AspNet.Mvc.HttpOkObjectResult to 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
        }
    });
}

最佳答案

您需要将IHttpActionResult 更改为IActionResult

Controller.Ok() 返回 HttpOkObjectResult ...

public class HttpOkObjectResult : ObjectResult, IActionResult {...}

...而您的方法签名定义了 IHttpActionResult,它来自以前的版本

关于c# - WebAPI One Asp.Net (ASP5/vNext) 中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36961976/

相关文章:

c# - Linq 查询在几分之一秒内运行,但 .ToList() 需要 3.5 秒

c# - 委托(delegate)和 Func Cast 问题

c# - 如何缓存处理程序文件和图像?

c# - 命中断点时执行代码?

c# - 在 C# 中强制实现通用接口(interface)

c# - 在C#中读取用C格式化的文件

javascript - onclick 函数中不止一个命令

c# - 实例化类会在 Assembly.GetTypes() 处产生 ReflectionTypeLoadException

c# - Windows 7 上的端口转发

c# - 使用基类 IEqualityComparer 执行 Distinct(),并且仍然返回子类类型?