RouteTable.Routes.GetVirtualPath() returns wrong route in ASP.NET MVC
I got the following routes:
// pennames
routes.MapRoute(
"pennames", // Route name
"MyHome/Authors/{action}/{id}", // URL with parameters
new { controller = "Author", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// article
routes.MapRoute(
"article", // Route name
"MyHome/Articles/{action}/{id}", // URL with parameters
new { controller = "Article", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// index
routes.MapRoute(
"MyHome", // Route name
"MyHome/{action}/{id}", // URL with parameters
new { controller = "MyHome", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I'm trying to add paging functionality to my "MyHome/Articles/" view. The pager is using the following code to get th开发者_Python百科e URL:
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;
}
The problem is, although I'm running this code from "MyHome/Articles/" page the GetVirtualPath
always returns the first route "MyHome/Authors/" and completely ignores the prefix "MyHome/Articles/" in the route.
When I use the ActionLink
it works fine, just not from GetVirtualPath
.
Any ideas?
Thanks.
If you want to apply pagination to your MyHome/Articles
You can give a hardcoded path like
yourpath="MyHome/Articles?page=" + page_number;
and each time you have to receive the page parameter from view with that pagination helper available for view or with the <%=Html.ActionLink()%> and pass routes parameter within this.
精彩评论