I am using asp.net mvc 2 and create localization based on routes.
我正在使用asp.net mvc 2并根据路线创建本地化。
- my route looks like:
{culture}/{controller}/{action}
- 我的路线如下:{culture} / {controller} / {action}
- I go to my home controller:
en/Home/Index
- 我去我的家庭控制器:en / Home / Index
-
my home controller view have a links to other controllers:
我的家庭控制器视图有一个到其他控制器的链接:
<a href='<%= Url.Action("Prods","Products") %>' >Products</a> <a href='<%= Url.Action("Index","About") %>' >About</a>
First link generated code: /en/Products/Prods
but second one generate: /Home/Index
第一个链接生成代码:/ en / Products / Prods但第二个生成:/ Home / Index
I can't understand why Url.Action
skips the {culture}
route parameter when I pass value Index
in argument action? What am I doing wrong?
当我在参数动作中传递值Index时,我无法理解为什么Url.Action会跳过{culture} route参数?我究竟做错了什么?
Route configuration:
路线配置:
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));
}
}
Thanks for all help
谢谢你的帮助
2 个解决方案
#1
43
When generating URL your best options is to always use the route names because this way you do not get into the subtleties of the algorithm used to select the route that will be used to generate the URL.
生成URL时,最好的选择是始终使用路由名称,因为这样您就不会深入了解用于选择将用于生成URL的路由的算法的细微之处。
My advice if for you not to use Url.Action
and instead use Url.RouteUrl
which allows you to specify the name of the route that should be used to construct the URL.
我的建议是,如果您不使用Url.Action,而是使用Url.RouteUrl,它允许您指定应该用于构造URL的路由的名称。
By always using route names you also make your code more robust to changes, for example, you can add new routes without having to worry that they might break your existing code for URL generation.
通过始终使用路由名称,您还可以使代码对更改更加健壮,例如,您可以添加新路由,而不必担心它们可能会破坏现有的URL生成代码。
#2
1
For purposes of building a Url.Action link, any Index
action without an id
token will match the Login
route. The Login route uses the SingleCultureMvcRouteHandler, so the culture will not be prepended to those links.
出于构建Url.Action链接的目的,任何没有id标记的Index操作都将与Login路由匹配。 Login路由使用SingleCultureMvcRouteHandler,因此不会将文化预先添加到这些链接。
#1
43
When generating URL your best options is to always use the route names because this way you do not get into the subtleties of the algorithm used to select the route that will be used to generate the URL.
生成URL时,最好的选择是始终使用路由名称,因为这样您就不会深入了解用于选择将用于生成URL的路由的算法的细微之处。
My advice if for you not to use Url.Action
and instead use Url.RouteUrl
which allows you to specify the name of the route that should be used to construct the URL.
我的建议是,如果您不使用Url.Action,而是使用Url.RouteUrl,它允许您指定应该用于构造URL的路由的名称。
By always using route names you also make your code more robust to changes, for example, you can add new routes without having to worry that they might break your existing code for URL generation.
通过始终使用路由名称,您还可以使代码对更改更加健壮,例如,您可以添加新路由,而不必担心它们可能会破坏现有的URL生成代码。
#2
1
For purposes of building a Url.Action link, any Index
action without an id
token will match the Login
route. The Login route uses the SingleCultureMvcRouteHandler, so the culture will not be prepended to those links.
出于构建Url.Action链接的目的,任何没有id标记的Index操作都将与Login路由匹配。 Login路由使用SingleCultureMvcRouteHandler,因此不会将文化预先添加到这些链接。