ASP.NET MVC - 本地化路线 [英] ASP.NET MVC - Localization route

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

问题描述

我想为我的网站创建本地化的 URL.它们显然应该指向相同的控制器操作,但我希望第一个路由值 - 始终 - 是位置/语言规范.这可能吗?

i'd like to create localized URL's for my site. They should obviously point to the same controller actions, but I want the first routevalues to -always- be the location/language specification. Is this possible?

http://www.website.com/en/us/控制器/动作

http://www.website.com/en/gb/控制器/动作

我知道这可以通过在每条路线中定义 {language}{location} 来完成,但我正在寻找一个巧妙的、非黑客的解决方案.

I understand it can be done by defining {language} and {location} in every route, but i'm looking for a slick, non-hacky solution.

推荐答案

您可以像这样创建一条将文化融入其中的路线...

You can create a route that has the culture built into it like this...

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",                                              // Route name
        "{culture}/{controller}/{action}/{id}",                           // URL with parameters
        new { culture="en-US", controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

}

您可以通过向所有这样的操作添加文化参数来获取文化...

You can get the culture by adding a culture parameter to all your actions like this...

public ActionResult Index(string culture)
{
    ViewData["Message"] = "Welcome to ASP.NET MVC! (" + culture + ")";

    return View();
}

您也可以解析 Global.asax 中 Application_BeginRequest 方法中的 URL 并在那里设置线程文化(下面的代码示例显示了如何设置文化,解析我留给您).

You can also probably parse the URL in the Application_BeginRequest method in Global.asax and set the threads culture there (code sample below shows how to set the culture, the parsing I leave to you).

如果您这样做,您可能无法使用 RedirectToAction 和 HTML.ActionLink 类型的方法,因为它们对文化一无所知.当然,您始终可以自己编写.

If you do this you will probably not be able to use the RedirectToAction and HTML.ActionLink type of methods since those don't know anything about cultures. Of course you could always write your own.

使用 url 存储文化的缺点是,如果您错过了网站某处的链接,或者用户离开网站然后又回来了,您可能会失去用户文化,他们将不得不重新设置(不是世界末日,而是烦人.使用 url 来存储文化的一个好处可能是 Google 会索引所有不同的语言.

The downside to using the url to store the culture is that if you miss a link somewhere on your website or the user leaves the website and then comes back, you could lose the users culture and they will have to set it again (not the end of the world, but annoying. Possibly a good side of using the url to store the culture is that Google will index all the different languages.

如果您更关心用户体验或开发的易用性,而不是 Google 将不同文化编入索引(实际上取决于您正在构建的网站类型),我建议将文化存储在 cookie 或会话状态中.

If you are more concerned about user experience or ease of development over Google indexing different cultures (really depends on what kind of site you are building), I would suggest storing the culture in a cookie or session state.

查看如何本地化 ASP .Net MVC 应用程序?.接受的答案指向博客文章这显示了如何本地化 ASP.Net 应用程序.

Check out How to localize ASP .Net MVC application?. The accepted answer points to a blog post that shows how you can localize an ASP.Net application.

如果您将用户选择的区域性存储在 cookie、会话状态或查询参数中,然后在 Global.asax 文件的 BeginRequest 方法中设置线程区域性.然后使用标准的 Microsoft 本地化程序集完成本地化.

If you store the culture the user selects in a cookie, session state, or query parameter and then set the threads culture in the BeginRequest method in the Global.asax file. Then localization is done using the standard Microsoft localization assemblies.

以下代码将允许您通过简单地添加culture=?? 随时更改文化.到查询字符串 (MyPage?culture=es-MX).然后它会被添加到 cookie 中,这样您就不需要将它添加到系统中每个链接的末尾.

The following code will allow you to change the culture at any time by simply adding culture=?? to the query string (MyPage?culture=es-MX). It will then be added to a cookie so that you don't need to add it to the end of every link in your system.

protected void Application_BeginRequest()
{
    var culture = Request["culture"] ?? Request.Cookies["culture"]?.Name;
    if (culture == null) culture = "en-US";
    var ci = CultureInfo.GetCultureInfo(culture);

    Thread.CurrentThread.CurrentCulture = ci;
    Thread.CurrentThread.CurrentUICulture = ci;

    var cookie = new HttpCookie("culture", ci.Name);
    Response.Cookies.Add(cookie);
}

这篇关于ASP.NET MVC - 本地化路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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