How can I use a URL to set the current culture in an ASP.NET 2.0 web application?
I am looking for ways to enable the site to basically have something like:
http://mysite.com/ ru-US / index.aspx`
Where "en-US" can vary by culture.
This culture in the url will basically set CurrentUICulture
for the app.
Basically, we currently have a page where the user explicitly clicks on it, but some of them are secondary and this causes some problems.
I know this kind of thing is easy to do in ASP.NET MVC, but what about those of us still working in version 2.0? Can you guys, in all your wisdom, suggest any suggestions / pointers / NOTHING that might get me started? This is new to me :)
I'm sure there must be some way to pick up the request and install / scan as needed. HttpModule
may be?
Update
I just thought. Your best bet is to create VirtDirs in IIS and then pull the relevant part from the requested url and set the culture to InitializeCulture
?
source to share
Does the selection in the cookie remain out of the question? It's nice that you offer users a choice, but why not just default to the user's client / web browser settings?
If they bookmark the page and lose the cookie, you can revert to the default, and if it's a culture that you don't support, then back up further to en-US.
If you want to keep your solution, you can use the overwrite mechanism. I've used http://www.managedfusion.com/products/url-rewriter/ in the past. For a list of engines see http://en.wikipedia.org/wiki/Rewrite_engine#IIS
source to share
You can easily use the routing feature designed for MVC with web forms. This SO question asks the question:
ASP.NET Routing with Web Forms
If you can't use the 3.5 framework, there are several URL rewriting modules out there. I have no experience in being able to make a recommendation.
source to share
I am doing this on some sites using ASP.net routing.
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 the 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
Finally, I use default.aspx in the root directory to redirect to the default lang used in the browser list.
Maybe it can be done with route.Defaults, but doesn't work inside Visual Studio (it might work on 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
Some sources:
* Mike Ormond's blog
* Chris Kavanagh's blog
* MSDN
source to share