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

Best way to handle URLs in a multilingual site in ASP.net(在 ASP.net 中处理多语言站点中 URL 的最佳方法)

本文介绍了在 ASP.net 中处理多语言站点中 URL 的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个多语言的网站,网址如

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

以前我会在IIS中设置两个虚拟目录,然后在global.aspx中检测URL,根据URL改变语言

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

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

解决方案更像是一种 hack.我正在考虑将路由用于新网站.

The solution is more like a hack. I'm thinking about using Routing for a new website.

你知道更好或更优雅的方法吗?

问题是关于 URL 处理,而不是资源等.

edit: The question is about the URL handling, not about resources, etc.

推荐答案

我决定使用新的 ASP.net Routing.
为什么不url重写?因为我不想更改路由提供给您的干净 URL.

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.

代码如下:

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

然后是 LangRouteHandler.vb 类:

Then LangRouteHandler.vb class:

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

最后我使用根目录中的 default.aspx 重定向到浏览器列表中使用的默认语言.
也许这可以通过 route.Defaults 完成,但在 Visual Studio 中不起作用(也许它在服务器中起作用)

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)

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

一些来源:
* 迈克·奥蒙德的博客
* 克里斯·卡瓦纳的博客
* MSDN

这篇关于在 ASP.net 中处理多语言站点中 URL 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 ASP.net 中处理多语言站点中 URL 的最佳方法