I am using Martijn Bolands paging and I have an issue with GetVirtualPathForArea method.
I have two pages: /Page1 and /Page2.
I’ve set a MapRoute for /Page1 and is accessed as /Page1Name
routes.MapRoute(
"Page1 name", // Route name
"Page1Name", // URL with parameters
new { controller = "ContollerName", action = "ActionName" } // Parameter defaults
);
If I try to get the Vitrual Path from Page1Name page, it gives me the correct one.
But!! If I try to get the Virtual Path from page2, it gives me again the Page1Name.
I haven’t set any Route for Page2 and If do this, then in Page1 I will get the VirtualPath of Page2.
Call and Methods
The call is through an HtmlHelper as follows
public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary, string controllerName) {
if (valuesDictionary == null) {
valuesDictionary = new RouteValueDictionary();
}
if (actionName != null) {
if (valuesDictionary.ContainsKey("action")) {
throw new ArgumentException("The valuesDictionary already contains an action.", "actionName");
}
valuesDictionary.Add("action", actionName);
}
var pager = new Pager(htmlHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary, null, controllerName);
return pager.RenderHtml();
}
This is from RenderHtml() method
It gets the viewContext from htmlHelper and this is the call to get the virtualPath
var pageLinkValueDictionary = new RouteValueDictionary(linkWithoutPageValuesDictionary) { { "page", pageNumber } };
var virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext, pageLinkValueDictionary);
Is it a known issue? Have I set something in a wrong way?
Thanks and I’ll appreciate any comment.
Response from microsoft:
The behavior you’re mentioning here is actually “by design”, if a route URL (such as your first route: FirstPage) has no parameters, no defaults, and no constraints, the call to RouteTable.Routes.GetVirtualPathForArea() will match this route (when it gets to it as it tries to match the route one by one in the order they are added).
The general fix is to make one of those conditions false OR to use named routes.
You can read more about this in a blog post by Phil Haack here: http://haacked.com/archive/2010/11/21/named-routes-to-the-rescue.aspx