I’ve created a system in MVC 3 using the NerdDinner tutorial as a base to work off. I’m not sure I fully understand Routing.
Everything was working fine until I added a sort to the Pagination helper that I have.
Here is the global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"UpcomingKeyDates", // Route name
"KeyDates.mvc/{sortBy}/Page/{page}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
);
routes.MapRoute(
"Root", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index", sortBy = "EventDate" } // Parameter defaults
);
}
I want to default the list to sort by Event Date ascending when you first navigate to the page (which works fine). The sort and pagination also works fine. However, when I’m using this link…
<%: Html.ActionLink("Create New", "Create", "Home") %>
The link just directs to the same page. Do I need to add a new route, or amend an existing route? Any help much appreciated.
Thanks.
The default route should always appear last and is the catch-all route. It will automatically catch the empty route which is equivalent to
http://yourdomain.com/The default route should always have the following format
Also, if the page is going to be a number, you can constrain it using a regular expression (see below).
In brief, change your
Global.asaxso it looks like this: