Martin
Martin

Reputation: 109

Pagination in WebAPI One Asp.Net (ASP5 / vNext)

I'm trying to make pagination in a Customer controller using ASP 5.0 WebAPI. When I try to do this example I get:

Cannot implicitly convert type Microsoft.AspNet.Mvc.HttpOkObjectResult to API.Controllers.IHttpActionResult

I just need to return a collection of customers with the number of the current page, total of pages and results.

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
        }
    });
}

Upvotes: 3

Views: 216

Answers (1)

Nkosi
Nkosi

Reputation: 247471

You need to change IHttpActionResult to IActionResult

Controller.Ok() returns HttpOkObjectResult...

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

...while your method signature defines IHttpActionResult, which is from the previous version

Upvotes: 2

Related Questions