Mark
Mark

Reputation: 7818

ASP.Net MVC Adding a Pager but route can't be found

I'm having trouble with adding a new route, to allow me to do paging.

In my Route.Config.cs file I have added a new route, UpcomingOffers:

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
           name: "UpcomingOffers",
           url: "Offer/Page/{page}",
           defaults: new { controller = "Offer", action = "Index" }
       );
    }
}

My Global.asax.cs file has, under Application_Start:

 RouteConfig.RegisterRoutes(RouteTable.Routes);

In my Offer controller I have:

    //
    // GET: /Offer/
    // GET: /Offer/Page/2

    public ActionResult Index(int? page)
    {
        const int pageSize = 10;
        var item = db.Customers.OrderByDescending(x => x.OfferCreatedOn).ToList();
        var paginatedItems = item.Skip((page ?? 0) * pageSize)
            .Take(pageSize)
            .ToList();
        return View(paginatedItems);
    }

But when I navigate to http://localhost:64296/offer/page/1 - I get the error message:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /offer/page/1

Can anyone see what I've done wrong? I suspect it's in my routes somewhere...

Thank you,

Mark

Upvotes: 1

Views: 1324

Answers (2)

Egor4eg
Egor4eg

Reputation: 2708

MVC uses the first valid root which matches the URL. You URL matches the first rought {controller}/{action}/{id}. That's why it tries to find a Controler=Offer and Action=Page.

Just swap you root registrations in your global.asax

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
       name: "UpcomingOffers",
       url: "Offer/Page/{page}",
       defaults: new { controller = "Offer", action = "Index" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Upvotes: 2

Darren Wainwright
Darren Wainwright

Reputation: 30727

Swap your 2 routes around. The order of the routes that you add to the route table is important. the new custom route is added before the existing Default route. If you reversed the order, then the Default route always will get called instead of the custom route.

        routes.MapRoute(
           name: "UpcomingOffers",
           url: "Offer/Page/{page}",
           defaults: new { controller = "Offer", action = "Index" }
       );

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

More info on custom routes here http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs

Upvotes: 8

Related Questions