I need to do a multilingual website, with urls like
我需要做一个多语种的网站,网址就像
www.domain.com/en/home.aspx for english
www.domain.com/es/home.aspx for spanish
In the past, I would set up two virtual directories in IIS, and then detect the URL in global.aspx and change the language according to the URL
在过去,我会在IIS中设置两个虚拟目录,然后在global.aspx中检测URL并根据URL更改语言
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim lang As String
If HttpContext.Current.Request.Path.Contains("/en/") Then
lang = "en"
Else
lang = "es"
End If
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End Sub
The solution is more like a hack. I'm thinking about using Routing for a new website.
解决方案更像是黑客攻击。我正在考虑使用Routing来建立一个新网站。
Do you know a better or more elegant way to do it?
你知道更好或更优雅的方式吗?
edit: The question is about the URL handling, not about resources, etc.
编辑:问题是关于URL处理,而不是关于资源等。
7 个解决方案
#1
7
I decided to go with the new ASP.net Routing.
Why not urlRewriting? Because I don't want to change the clean URL that routing gives to you.
我决定使用新的ASP.net路由。为什么不urlRewriting?因为我不想更改路由提供给您的干净URL。
Here is the code:
这是代码:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
RegisterRoutes(RouteTable.Routes)
End Sub
Public Sub RegisterRoutes(ByVal routes As RouteCollection)
Dim reportRoute As Route
Dim DefaultLang As String = "es"
reportRoute = New Route("{lang}/{page}", New LangRouteHandler)
'* if you want, you can contrain the values
'reportRoute.Constraints = New RouteValueDictionary(New With {.lang = "[a-z]{2}"})
reportRoute.Defaults = New RouteValueDictionary(New With {.lang = DefaultLang, .page = "home"})
routes.Add(reportRoute)
End Sub
Then LangRouteHandler.vb class:
然后LangRouteHandler.vb类:
Public Class LangRouteHandler
Implements IRouteHandler
Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) As System.Web.IHttpHandler _
Implements System.Web.Routing.IRouteHandler.GetHttpHandler
'Fill the context with the route data, just in case some page needs it
For Each value In requestContext.RouteData.Values
HttpContext.Current.Items(value.Key) = value.Value
Next
Dim VirtualPath As String
VirtualPath = "~/" + requestContext.RouteData.Values("page") + ".aspx"
Dim redirectPage As IHttpHandler
redirectPage = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, GetType(Page))
Return redirectPage
End Function
End Class
Finally I use the default.aspx in the root to redirect to the default lang used in the browser list.
Maybe this can be done with the route.Defaults, but don't work inside Visual Studio (maybe it works in the server)
最后,我使用root中的default.aspx重定向到浏览器列表中使用的默认lang。也许这可以通过route.Defaults来完成,但是在Visual Studio中不起作用(也许它可以在服务器中运行)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DefaultLang As String = "es"
Dim SupportedLangs As String() = {"en", "es"}
Dim BrowserLang As String = Mid(Request.UserLanguages(0).ToString(), 1, 2).ToLower
If SupportedLangs.Contains(BrowserLang) Then DefaultLang = BrowserLang
Response.Redirect(DefaultLang + "/")
End Sub
Some sources:
* Mike Ormond's blog
* Chris Cavanagh’s Blog
* MSDN
一些消息来源:* Mike Ormond的博客* Chris Cavanagh的Blog * MSDN
#2
1
- Use urlrewriteing.net for asp.net webforms, or routing with mvc. Rewrite www.site.com/en/something.aspx to url: page.aspx?lang=en.
UrlRewriteing.net can be easily configured via regex in web.config. You can also use routing with webforms now, it's probably similar... - with webforms, let every aspx page inherits from BasePage class, which then inherits from Page class.
In BasePage class override "InitializeCulture()" and set culture info to thread, like you described in question.
It's good to do that in this order: 1. check url for Lang param, 2. check cookie, 3. set default lang - For static content (text, pics url) on pages use LocalResources,or Global if content is repeating across site. You can watch videocast on using global/local res. on www.asp.net
- Prepare db for multiple languages. But that's another story.
将urlrewriteing.net用于asp.net webforms,或使用mvc进行路由。将www.site.com/en/something.aspx重写为url:page.aspx?lang = en。可以通过web.config中的regex轻松配置UrlRewriteing.net。你现在也可以使用webforms路由,它可能类似......
使用webforms,让每个aspx页继承自BasePage类,然后继承自Page类。在BasePage类中重写“InitializeCulture()”并将文化信息设置为线程,就像您所描述的那样。按顺序执行此操作是很好的:1。检查Lang param的URL,2。检查cookie,3。设置默认lang
对于页面上的静态内容(文本,图片URL),使用LocalResources,如果内容在网站上重复,则使用Global。您可以使用全局/本地资源观看视频广播。在www.asp.net上
为多种语言准备db。但这是另一个故事。
#4
0
UrlRewriting is the way to go.
UrlRewriting是要走的路。
There is a good article on MSDN on the best ways to do it.
MSDN上有一篇关于最佳方法的文章。
#5
0
Kind of a tangent, but I'd actually avoid doing this with different paths unless the different languages are completely content separate from each other.
切线的类型,但我实际上避免使用不同的路径执行此操作,除非不同的语言完全相互分离。
For Google rank, or for users sharing URLs (one of the big advantages of ‘clean’ URLs), you want the address to stay as constant as possible.
对于Google排名或共享网址的用户(“干净”网址的一大优势),您希望地址尽可能保持不变。
You can find users’ language preferences from their browser settings:
您可以从浏览器设置中找到用户的语言首选项:
CultureInfo.CurrentUICulture
Then your URL for English or Spanish:
然后是您的英语或西班牙语的URL:
www.domain.com/products/newproduct
Same address for any language, but the user gets the page in their chosen language.
任何语言的地址相同,但用户以所选语言获取页面。
We use this in Canada to provide systems in English and French at the same time.
我们在加拿大使用它来同时提供英语和法语系统。
#6
0
To do this with URL Routing, refer to this post:
要使用URL路由执行此操作,请参阅此帖子:
Friendly URLS with URL Routing
带URL路由的友好URLS
#7
0
Also, watch out new IIS 7.0 - URL Rewriting. Excellent article here http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
另外,请注意新的IIS 7.0 - URL重写。这里有优秀的文章http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
I liked this part Which Option Should You Use?
我喜欢这个部分你应该使用哪个选项?
-
If you are developing a new ASP.NET Web application that uses either ASP.NET MVC or ASP.NET Dynamic Data technologies, use ASP.NET routing. Your application will benefit from native support for clean URLs, including generation of clean URLs for the links in your Web pages. Note that ASP.NET routing does not support standard Web Forms applications yet, although there are plans to support it in the future.
如果您正在开发使用ASP.NET MVC或ASP.NET动态数据技术的新ASP.NET Web应用程序,请使用ASP.NET路由。您的应用程序将受益于对干净URL的本机支持,包括为网页中的链接生成干净的URL。请注意,ASP.NET路由不支持标准Web窗体应用程序,尽管计划在将来支持它。
-
If you already have a legacy ASP.NET Web application and do not want to change it, use the URL-rewrite module. The URL-rewrite module allows you to translate search-engine-friendly URLs into a format that your application currently uses. Also, it allows you to create redirect rules that can be used to redirect search-engine crawlers to clean URLs. http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
如果您已有旧的ASP.NET Web应用程序但不想更改它,请使用URL-rewrite模块。 URL重写模块允许您将搜索引擎友好的URL转换为应用程序当前使用的格式。此外,它还允许您创建可用于将搜索引擎抓取工具重定向到干净URL的重定向规则。 http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Thanks, Maulik.
#1
7
I decided to go with the new ASP.net Routing.
Why not urlRewriting? Because I don't want to change the clean URL that routing gives to you.
我决定使用新的ASP.net路由。为什么不urlRewriting?因为我不想更改路由提供给您的干净URL。
Here is the code:
这是代码:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
RegisterRoutes(RouteTable.Routes)
End Sub
Public Sub RegisterRoutes(ByVal routes As RouteCollection)
Dim reportRoute As Route
Dim DefaultLang As String = "es"
reportRoute = New Route("{lang}/{page}", New LangRouteHandler)
'* if you want, you can contrain the values
'reportRoute.Constraints = New RouteValueDictionary(New With {.lang = "[a-z]{2}"})
reportRoute.Defaults = New RouteValueDictionary(New With {.lang = DefaultLang, .page = "home"})
routes.Add(reportRoute)
End Sub
Then LangRouteHandler.vb class:
然后LangRouteHandler.vb类:
Public Class LangRouteHandler
Implements IRouteHandler
Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) As System.Web.IHttpHandler _
Implements System.Web.Routing.IRouteHandler.GetHttpHandler
'Fill the context with the route data, just in case some page needs it
For Each value In requestContext.RouteData.Values
HttpContext.Current.Items(value.Key) = value.Value
Next
Dim VirtualPath As String
VirtualPath = "~/" + requestContext.RouteData.Values("page") + ".aspx"
Dim redirectPage As IHttpHandler
redirectPage = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, GetType(Page))
Return redirectPage
End Function
End Class
Finally I use the default.aspx in the root to redirect to the default lang used in the browser list.
Maybe this can be done with the route.Defaults, but don't work inside Visual Studio (maybe it works in the server)
最后,我使用root中的default.aspx重定向到浏览器列表中使用的默认lang。也许这可以通过route.Defaults来完成,但是在Visual Studio中不起作用(也许它可以在服务器中运行)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DefaultLang As String = "es"
Dim SupportedLangs As String() = {"en", "es"}
Dim BrowserLang As String = Mid(Request.UserLanguages(0).ToString(), 1, 2).ToLower
If SupportedLangs.Contains(BrowserLang) Then DefaultLang = BrowserLang
Response.Redirect(DefaultLang + "/")
End Sub
Some sources:
* Mike Ormond's blog
* Chris Cavanagh’s Blog
* MSDN
一些消息来源:* Mike Ormond的博客* Chris Cavanagh的Blog * MSDN
#2
1
- Use urlrewriteing.net for asp.net webforms, or routing with mvc. Rewrite www.site.com/en/something.aspx to url: page.aspx?lang=en.
UrlRewriteing.net can be easily configured via regex in web.config. You can also use routing with webforms now, it's probably similar... - with webforms, let every aspx page inherits from BasePage class, which then inherits from Page class.
In BasePage class override "InitializeCulture()" and set culture info to thread, like you described in question.
It's good to do that in this order: 1. check url for Lang param, 2. check cookie, 3. set default lang - For static content (text, pics url) on pages use LocalResources,or Global if content is repeating across site. You can watch videocast on using global/local res. on www.asp.net
- Prepare db for multiple languages. But that's another story.
将urlrewriteing.net用于asp.net webforms,或使用mvc进行路由。将www.site.com/en/something.aspx重写为url:page.aspx?lang = en。可以通过web.config中的regex轻松配置UrlRewriteing.net。你现在也可以使用webforms路由,它可能类似......
使用webforms,让每个aspx页继承自BasePage类,然后继承自Page类。在BasePage类中重写“InitializeCulture()”并将文化信息设置为线程,就像您所描述的那样。按顺序执行此操作是很好的:1。检查Lang param的URL,2。检查cookie,3。设置默认lang
对于页面上的静态内容(文本,图片URL),使用LocalResources,如果内容在网站上重复,则使用Global。您可以使用全局/本地资源观看视频广播。在www.asp.net上
为多种语言准备db。但这是另一个故事。
#3
#4
0
UrlRewriting is the way to go.
UrlRewriting是要走的路。
There is a good article on MSDN on the best ways to do it.
MSDN上有一篇关于最佳方法的文章。
#5
0
Kind of a tangent, but I'd actually avoid doing this with different paths unless the different languages are completely content separate from each other.
切线的类型,但我实际上避免使用不同的路径执行此操作,除非不同的语言完全相互分离。
For Google rank, or for users sharing URLs (one of the big advantages of ‘clean’ URLs), you want the address to stay as constant as possible.
对于Google排名或共享网址的用户(“干净”网址的一大优势),您希望地址尽可能保持不变。
You can find users’ language preferences from their browser settings:
您可以从浏览器设置中找到用户的语言首选项:
CultureInfo.CurrentUICulture
Then your URL for English or Spanish:
然后是您的英语或西班牙语的URL:
www.domain.com/products/newproduct
Same address for any language, but the user gets the page in their chosen language.
任何语言的地址相同,但用户以所选语言获取页面。
We use this in Canada to provide systems in English and French at the same time.
我们在加拿大使用它来同时提供英语和法语系统。
#6
0
To do this with URL Routing, refer to this post:
要使用URL路由执行此操作,请参阅此帖子:
Friendly URLS with URL Routing
带URL路由的友好URLS
#7
0
Also, watch out new IIS 7.0 - URL Rewriting. Excellent article here http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
另外,请注意新的IIS 7.0 - URL重写。这里有优秀的文章http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
I liked this part Which Option Should You Use?
我喜欢这个部分你应该使用哪个选项?
-
If you are developing a new ASP.NET Web application that uses either ASP.NET MVC or ASP.NET Dynamic Data technologies, use ASP.NET routing. Your application will benefit from native support for clean URLs, including generation of clean URLs for the links in your Web pages. Note that ASP.NET routing does not support standard Web Forms applications yet, although there are plans to support it in the future.
如果您正在开发使用ASP.NET MVC或ASP.NET动态数据技术的新ASP.NET Web应用程序,请使用ASP.NET路由。您的应用程序将受益于对干净URL的本机支持,包括为网页中的链接生成干净的URL。请注意,ASP.NET路由不支持标准Web窗体应用程序,尽管计划在将来支持它。
-
If you already have a legacy ASP.NET Web application and do not want to change it, use the URL-rewrite module. The URL-rewrite module allows you to translate search-engine-friendly URLs into a format that your application currently uses. Also, it allows you to create redirect rules that can be used to redirect search-engine crawlers to clean URLs. http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
如果您已有旧的ASP.NET Web应用程序但不想更改它,请使用URL-rewrite模块。 URL重写模块允许您将搜索引擎友好的URL转换为应用程序当前使用的格式。此外,它还允许您创建可用于将搜索引擎抓取工具重定向到干净URL的重定向规则。 http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Thanks, Maulik.