RouteTable.Routes.GetVirtualPath() 在 ASP.NET MVC 中返回错误的路由
我有以下路线:
// 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
);
我正在尝试将分页功能添加到我的“MyHome/Articles/”视图中。寻呼机使用以下代码来获取 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;
}
问题是,尽管我从“MyHome/Articles/”页面运行此代码,但 GetVirtualPath
始终返回第一个路由“MyHome/Authors/” ”并完全忽略路由中的前缀“MyHome/Articles/”。
当我使用 ActionLink
时,它工作正常,只是不是来自 GetVirtualPath
。
有什么想法吗?
谢谢。
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 the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想对 MyHome/Articles 应用分页,
您可以提供硬编码路径,例如
yourpath="MyHome/Articles?page=" + page_number;
每次您必须使用可用于查看的分页助手或使用 <%=Html.ActionLink()%> 从视图接收页面参数时并在其中传递路由参数。
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.