I am following Martijn Boland's 'Paging with ASP.NET MVC'. And while helpful it has raised a couple of issues I don't understand.
Martijn says:
Internally, the pager uses RouteTable.Routes.GetVirtualPath() to render the url’s so the page url’s can be configured via routing to create nice looking url’s like for example ‘/Categories/Shoes/Page/1′ instead of ‘/Paging/ViewByCategory?name=Shoes&page=1′.
This is the is what he is talking about:
private string GeneratePageLink(string linkText, int pageNumber)
{
var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
pageLinkValueDictionary.Add("page", pageNumber);
//var virtualPathData = this.viewContext.RouteData.Route.GetVirtualPath(this.viewContext, pageLinkValueDictionary);
var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);
if (virtualPathData != null)
{
string linkFormat = "<a href=\"{0}\">{1}</a>";
return String.Format(linkFormat, virtualPathData.VirtualPath, linkText);
}
else
{
return null;
}
}
How does this work? When I use it virtualPathData.VirtualPath just brings back a url representing the first route in my routing table with a 'page' param on the end rather then a url representing the current context.
Also what would the routing look like to change this ‘/Paging/ViewByCategory?name=Shoes&page=1′ to this ‘/Categories/Shoes/Page/1′ ?