简体   繁体   中英

WebApi Pagination not working, page is always 0 even when I set url parameter correctly

The api controller method is this:

        public IHttpActionResult GettblEmpleados(int page, int pageSize)
        {
            var query = db.SPEmpleadosIntranet().ToList();

            var totalCount = query.Count();
            var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);

            var urlHelper = new UrlHelper(Request);
            var prevLink = page > 0 ? urlHelper.Link("Empleados", new { page = page - 1, pageSize = pageSize }) : "";
            var nextLink = page < totalPages - 1 ? urlHelper.Link("Empleados", new { page = page + 1, pageSize = pageSize }) : "";

            var results = query
            .Skip(pageSize * page)
            .Take(pageSize)
            .ToList();

            return Ok(query);
        }

And the url I use to consume the service is:

However, page is always 0, then prevLink is always empty http://localhost:12929/api/Empleados?page=0&pageSize=10

The error I get is this: (sorry framework its in spanish)

Translatio says it cant find the path /Empleados

No se encuentra ninguna ruta con el nombre 'Empleados' en la colección de rutas.\\r\\nNombre del parámetro: name"

this is the controller

public class EmpleadosController : ApiController
{

and this is the globa.asax

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Formatters.Clear();
    GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);

Update1

this is webapiconfig

public static void Register(HttpConfiguration config) 
        { 
            // Web API configuration and services 
            // Web API routes 
            config.MapHttpAttributeRoutes(); 
            config.Routes.MapHttpRoute( 
                name: "DefaultApi", 
                routeTemplate: "api/{controller}/{id}", 
                defaults: new { id = RouteParameter.Optional } 
            ); 
        }

In your current URL you aren't referring to your action...

Controller / Action / Params

in your webapiconfig -->

routeTemplate: "api/{controller}/{action}/{id}"

http://localhost:12929/api/Empleados/GettblEmpleados?page=0&pageSize=10

You need to tell the method which Http method it matches to. Try decorating it with the following attribute:

[HttpGet]
public IHttpActionResult GettblEmpleados(int page, int pageSize)
{

Since you are using config.MapHttpAttributeRoutes(); you can use Route Prefixes as follows and because you are using the UrlHelper.Link Method you need to set the Route Name

[RoutePrefix("api/Empleados")]   
public class EmpleadosController : ApiController {

    [Route("", Name="Empleados")]
    [HttpGet]
    public IHttpActionResult GettblEmpleados(int page, int pageSize) {
        var query = db.SPEmpleadosIntranet().ToList();

        var totalCount = query.Count();
        var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);

        var urlHelper = new UrlHelper(Request);
        var prevLink = page > 0 ? urlHelper.Link("Empleados", new { page = page - 1, pageSize = pageSize }) : "";
        var nextLink = page < totalPages - 1 ? urlHelper.Link("Empleados", new { page = page + 1, pageSize = pageSize }) : "";

        var results = query
        .Skip(pageSize * page)
        .Take(pageSize)
        .ToList();

        return Ok(query);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM