ASP.NET MVC Url.Action和路由名称值 [英] ASP.NET MVC Url.Action and route name value

查看:114
本文介绍了ASP.NET MVC Url.Action和路由名称值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net MVC 2和创建基于路由的本地​​化。

I am using asp.net mvc 2 and create localization based on routes.


  1. 我的路线是这样的: {文化} / {控制器} / {行动}

  2. 我去我家的控制器: EN /首页/指数

  3. 我家的控制器认为有一个链接到其他的控制器:

  1. my route looks like: {culture}/{controller}/{action}
  2. I go to my home controller: en/Home/Index
  3. 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>


第一个环节产生code: / EN /产品/电棒,但第二个产生: /首页/指数

First link generated code: /en/Products/Prods but second one generate: /Home/Index

我不明白为什么 Url.Action 跳过 {文化} 路由参数当我通过值首页的说法行动?我在做什么错了?

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?

路由配置:

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
               );      

然后

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。

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.Action ,而使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.routeurl.aspx\"><$c$c>Url.RouteUrl它允许你指定要用于构造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.

通过始终使用路由名称也使你的code更稳健的变化,例如,你可以不必担心,他们可能会破坏你现有的code为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.

这篇关于ASP.NET MVC Url.Action和路由名称值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆