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

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

问题描述

我想创建本地化的URL为我的网站。他们显然应该指向同一个控制器的动作,但我想首先要routevalues​​是-always-位置/语言规范。这可能吗?

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 /我们 /控制器/动作

http://www.website.com/ EN / GB /控制器/动作

我的理解是可以通过在每条路由定义 {语言} {位置} 来完成,但我在找一个漂亮的,非哈克的解决方案。

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,并设置线程的文化有(code下面的示例显示了如何设置文化,分析我离开你)。

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来存储文化的好的一面是,谷歌将索引所有不同的语言。

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.

如果你更关心用户体验或易于开发过谷歌索引不同的文化(实际上取决于什么样的网站,你正在构建的),我会建议存储在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.

以下code可以让你通过简单地增加文化=改变在任何时候的文化?到查询字符串(我的页面?文化= 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"];
    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天全站免登陆