简体   繁体   English

如何在ASP.NET中使用请求URL / URL重写进行本地化-使用HTTP模块或Global.asax代码

[英]How to use the Request URL/URL Rewriting For Localization in ASP.NET - Using an HTTP Module or Global.asax Code

I wanted to see if there is a way to use the request URL/URL rewriting to set the language a page is rendered in by examining a portion of the URL in ASP.NET. 我想看看是否有一种方法可以通过检查ASP.NET中的URL的一部分来使用请求URL / URL重写来设置呈现页面的语言。 We have a site that already works with ASP.NET's resource localization and user's can change the language that they see pages/resources on the site in, however the current mechanism in not very search engine friendly since the language variations for each language all appear as one page. 我们有一个站点已经可以使用ASP.NET的资源本地化,并且用户可以更改他们在该站点上看到页面/资源所用的语言,但是当前机制对搜索引擎的友好程度不是很高,因为每种语言的语言都显示为一页。 It would be much better if we could have pages like www.site.com/en-mx/realfolder/realpage.aspx that allow linking to culture specific versions of a page. 如果我们拥有像www.site.com/en-mx/realfolder/realpage.aspx这样的页面,它们可以链接到特定于页面的区域版本,那就更好了。

I know lots of people have likely done localization through URL structures before and I wanted to know if one of your could share how to do this in the Global.asax file or with an HTTP Module (pointing to links to blog postings would be great too). 我知道很多人以前可能已经通过URL结构进行过本地化,而且我想知道你们中的一个人是否可以在Global.asax文件或HTTP模块中共享如何进行此操作(指向博客文章的链接也很不错) )。 We have a restriction that the site is based on ASP.NET 2.0 (so we can't used the 3.5+ features yet). 我们限制该站点基于ASP.NET 2.0(因此,我们尚不能使用3.5+功能)。

Here is the example scenario: 这是示例方案:

A real page exits at: www.site.com/realfolder/realpage.aspx 实际页面位于:www.site.com/realfolder/realpage.aspx

The page has a mechanism for the user to change the language it is displayed in via a dropdown. 该页面具有供用户通过下拉菜单更改其显示语言的机制。

There are search engine optimization and user links sharing benefits to doing this since people can link directly to a page that has content that is applicable to a certain language (this could also include right-to-left layouts for languages like Japanese). 搜索引擎优化和用户链接共享了这样做的好处,因为人们可以直接链接到具有适用于某种特定语言的内容的页面(这也可能包括从日语到日语的从右到左的布局)。

I would like to use an HTTP module to see if the first part of the URL after www.site.com, site.com, subdomain.site.com, etc. contains a valid culture code (eg en-us, es-mx) then use that value to set the localization culture of the page/resources based on that URL. 我想使用HTTP模块查看www.site.com,site.com,subdomain.site.com等之后的URL的第一部分是否包含有效的区域性代码(例如en-us,es-mx ),然后使用该值基于该URL设置页面/资源的本地化区域性。

So if the user accesses the URL www.site.com/en-MX/realfolder/realpage.aspx 因此,如果用户访问URL www.site.com/en-MX/realfolder/realpage.aspx

Then the page will render in Mexico's variant of Spanish. 然后,页面将以墨西哥的西班牙语版本呈现。

If the user goes to www.site.com/realfolder/realpage.aspx directly the page would just use their browser's language settings. 如果用户直接访问www.site.com/realfolder/realpage.aspx,则该页面将仅使用其浏览器的语言设置。

Im doing it in the global.asax, as this answer explains , then I set up different urls in the master page to change the language depending on the current one: 我在global.asax中执行此操作,如答案所示 ,然后我在母版页中设置了不同的url来根据当前语言更改语言:

ie: 即:

    If Request.Path.Contains("/en/") Then
        DestinationPathEs = Request.Path.Replace("/en/", "/es/")
        DestinationPathPt = Request.Path.Replace("/en/", "/pt/")

        litLangBar.Text = "<span id='langbares'><a href='" & DestinationPathEs & "'>Español</a></span>"
        litLangBar.Text += "<span id='langbarpt'><a href='" & DestinationPathPt & "'>Portugues</a></span>"
        litLangBar.Text += "<span id='langbaren' class='active'>English</span>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Global.asax中的ASP.NET导入命名空间错误 - ASP.NET Import Namespace Error in Global.asax 获取 Global.asax 中的 ASP.NET MVC 区域 - Get ASP.NET MVC area in Global.asax 我们可以在asp.net中对global.asax进行单元测试吗? - Can we unit test global.asax in asp.net? 在ASP.NET MVC global.asax中放弃会话 - Abandon session in ASP.NET MVC global.asax 如果我不为WCF服务使用ASP.NET兼容性,可以忽略Global.asax文件吗? - Can Global.asax file be ignored if I don't use ASP.NET compatibility for a WCF Service? 代码分析建议在定义中使用const但在global.asax中如何使用? - Code Analysis recommends using const in definition but how in global.asax? 如何通过Global.asax使用Local_resources? - How to use Local_resources using Global.asax? 什么是Global.asax ASP.Net Web API中的GlobalConfiguration.Configuration.EnsureInitialized() - What is GlobalConfiguration.Configuration.EnsureInitialized() in Global.asax ASP.Net Web API 在Global.asax和Error.aspx中记录ASP.NET MVC错误 - ASP.NET MVC Error Logging in Both Global.asax and Error.aspx Global.asax中的ASP.NET MVC Application_Error处理程序 - ASP.NET MVC Application_Error handler in Global.asax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM