我正在使用 asp.net mvc 2 并根据路由创建本地化。
- 我的路线如下:
{culture}/{controller}/{action}
- 我转到我的家庭控制器:
en/Home/Index
-
我的家庭控制器视图有到其他控制器的链接:
<a href='<%= Url.Action("Prods","Products") %>' >Products</a>
<a href='<%= Url.Action("Index","About") %>' >About</a>
第一个链接生成的代码:/en/Products/Prods
但第二个生成:/Home/Index
我不明白为什么Url.Action
跳过{culture}
当我传递值时的路由参数Index
在论证行动中?我究竟做错了什么?
路由配置:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Login", // Route name
"{controller}/Index", // URL with parameters
new { controller = "Login", action = "Index" } // Parameter defaults
).RouteHandler = new SingleCultureMvcRouteHandler();
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Then
foreach (Route r in routes)
{
if (!(r.RouteHandler is SingleCultureMvcRouteHandler))
{
r.RouteHandler = new MultiCultureMvcRouteHandler();
r.Url = "{culture}/" + r.Url;
if (r.Defaults == null)
{
r.Defaults = new RouteValueDictionary();
}
r.Defaults.Add("culture", "en");
if (r.Constraints == null)
{
r.Constraints = new RouteValueDictionary();
}
r.Constraints.Add("culture", new CultureConstraint(cultures));
}
}
感谢所有帮助
生成 URL 时,最好的选择是始终使用路由名称,因为这样您就不会陷入用于选择将用于生成 URL 的路由的算法的微妙之处。
我的建议是如果你不要使用Url.Action
并改为使用Url.RouteUrl http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.routeurl.aspx它允许您指定用于构造 URL 的路由名称。
通过始终使用路由名称,您还可以使代码对更改更加稳健,例如,您可以添加新路由,而不必担心它们可能会破坏现有的 URL 生成代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)