在ASP.NET中使用MVC4路由进行本地化

时间:2013-11-21 11:55:47

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我正在尝试在我的网站上放置不同的语言,我真的不明白设置正确的路线以使其工作的方法。因为我现在拥有一个重定向循环。

所以这是代码:

routes.MapRoute(
      null,
      url: "{culture}/{pageName}",
      defaults: new
      {
        culture = UrlParameter.Optional,
        controller = "Redirect",
        action = "Index",
        pageName = "debug"
      });
        routes.MapRoute(
  null,
  "{pageName}/",
  new { controller = "Landing", action = "Index" }
  );
  routes.MapRoute(
  null,
  "{culture}/{pageName}/",
  new { controller = "Landing", action = "Index" },
  new { culture = "^[a-z]{2}" }
  );
  routes.MapRoute(
  null,
  "{culture}/blog/{pageName}",
  new { controller = "Blog", action = "Index" },
  new { culture = "^[a-z]{2}" }
  );
  routes.MapRoute(
  null,
  "blog/{pageName}",
  new { controller = "Blog", action = "Index" }
  );

问题是:如何正确设置路线以便以这种方式查看网站:

如果点击/pageName>转到/pageName(默认文化设置在之后设置为英语) 否则如果/en/pageName>重定向到/pageName 否则如果/AnyCulture/pageName>转到/AnyCulture/pageName 否则/incorrectUrl>回家

感谢您阅读/回答:)

1 个答案:

答案 0 :(得分:0)

您必须为区域性设置参数,以便将区域性识别为参数,并且相应的操作/控制器可以处理它,就像暗示您的控制器/操作具有这样的参数:

    routes.MapRoute(
      null,
      "{culture}/{pageName}/",
      new { controller = "Landing", action = "Index", culture = UrlParameter.Optional },
      new { culture = "^[a-z]{2}" }
      );