简体   繁体   English

在ASP.net中处理多语言网站中的URL的最佳方法

[英]Best way to handle URLs in a multilingual site in ASP.net

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. 我正在考虑将路由用于新网站。

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处理,而不是关于资源等。

I decided to go with the new ASP.net Routing. 我决定使用新的ASP.net路由。
Why not urlRewriting? 为什么不urlRewriting? Because I don't want to change the clean URL that routing gives to you. 因为我不想更改路由为您提供的干净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. 最后,我使用root中的default.aspx重定向到浏览器列表中使用的默认lang。
Maybe this can be done with the route.Defaults, but don't work inside Visual Studio (maybe it works in the server) 也许这可以通过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 * Mike Ormond的博客
* Chris Cavanagh's Blog * Chris Cavanagh的博客
* MSDN * MSDN

  1. Use urlrewriteing.net for asp.net webforms, or routing with mvc. 将urlrewriteing.net用于asp.net webforms,或使用mvc进行路由。 Rewrite www.site.com/en/something.aspx to url: page.aspx?lang=en. 将www.site.com/en/something.aspx重写为url:page.aspx?lang = en。
    UrlRewriteing.net can be easily configured via regex in web.config. 可以通过web.config中的regex轻松配置UrlRewriteing.net。 You can also use routing with webforms now, it's probably similar... 你现在也可以使用webforms路由,它可能类似......
  2. with webforms, let every aspx page inherits from BasePage class, which then inherits from Page class. 使用webforms,让每个aspx页继承自BasePage类,然后继承自Page类。
    In BasePage class override "InitializeCulture()" and set culture info to thread, like you described in question. 在BasePage类中重写“InitializeCulture()”并将文化信息设置为线程,就像您所描述的那样。
    It's good to do that in this order: 1. check url for Lang param, 2. check cookie, 3. set default lang 按此顺序执行此操作是很好的:1。检查Lang param的URL,2。检查cookie,3。设置默认lang
  3. For static content (text, pics url) on pages use LocalResources,or Global if content is repeating across site. 对于页面上的静态内容(文本,图片网址),使用LocalResources,如果内容在网站上重复,则使用全局。 You can watch videocast on using global/local res. 您可以使用全局/本地资源观看视频广播。 on www.asp.net 在www.asp.net上
  4. Prepare db for multiple languages. 为多种语言准备db。 But that's another story. 但这是另一个故事。

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 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. 我们在加拿大使用它来同时提供英语和法语系统。

To do this with URL Routing, refer to this post: 要使用URL路由执行此操作,请参阅此帖子:

Friendly URLS with URL Routing 带URL路由的友好URL

Also, watch out new IIS 7.0 - URL Rewriting. 另外,请注意新的IIS 7.0 - URL重写。 Excellent article here http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/ 这里有优秀的文章http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/

I liked this part Which Option Should You Use? 我喜欢这个部分你应该使用哪个选项?

  1. 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. 如果您正在开发使用ASP.NET MVC或ASP.NET动态数据技术的新ASP.NET Web应用程序,请使用ASP.NET路由。 Your application will benefit from native support for clean URLs, including generation of clean URLs for the links in your Web pages. 您的应用程序将受益于对干净URL的本机支持,包括为网页中的链接生成干净的URL。 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路由不支持标准Web窗体应用程序,尽管计划在将来支持它。

  2. If you already have a legacy ASP.NET Web application and do not want to change it, use the URL-rewrite module. 如果您已有旧的ASP.NET Web应用程序但不想更改它,请使用URL-rewrite模块。 The URL-rewrite module allows you to translate search-engine-friendly URLs into a format that your application currently uses. URL重写模块允许您将搜索引擎友好的URL转换为应用程序当前使用的格式。 Also, it allows you to create redirect rules that can be used to redirect search-engine crawlers to clean URLs. 此外,它还允许您创建可用于将搜索引擎抓取工具重定向到干净URL的重定向规则。 http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/ http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/

Thanks, Maulik. 谢谢,毛利克。

I personnaly use the resources files . 我个人使用资源文件

Very efficient, very simple. 非常高效,非常简单。

UrlRewriting is the way to go. UrlRewriting是要走的路。

There is a good article on MSDN on the best ways to do it. 在MSDN上有一篇关于最佳方法的文章。

http://msdn.microsoft.com/en-us/library/ms972974.aspx http://msdn.microsoft.com/en-us/library/ms972974.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM