I am starting a multilanguage effort. In Global.asax.cs, method Application_BeginRequest(), a cookie is read and CurrentUICulture is set to en-US. However, in MyPage.aspx the value has surprisingly changed to nl.
Now this web site I inherited, and is medium-sized and has a fairly complex menu system. Also, it maintains a user table with a field for Preferred Language with values like nl, but I could not find (yet) in the code where this sets CurrentUICulture from that user table.
This is a page with a MasterPage, so I looked over there. I set a debug breakpoint in the first page event in MasterPage.Page_Init(), and in the Immediate Window I inspected System.Threading.Thread.CurrentThread.CurrentUICulture.Name. Value is: 'nl'. I am completely puzzled.
My question is: what code can possibly execute between Global.asax.cs, Application_BeginRequest(), and MasterPage.Page_Init()?
The
Application_BeginRequest()handler is the first step in the Asp.Net life cycle. Look here:https://learn.microsoft.com/en-us/iis/application-frameworks/building-and-running-aspnet-applications/aspnet-integration-with-iis
When a handler is executing (which is your page), it has its own life cycle:
https://learn.microsoft.com/en-us/previous-versions/ms178472(v=vs.140)?redirectedfrom=MSDN
And this documentation says:
When the available events are checked, we see:
Therefore; it seems that the earliest stage you can change the
UICultureis thePreInitstage. The most appropriate place however, is theInitializeCulturemethod which serves this specific purpose:EDIT:
Despite being shared accross multiple pages, the culture information should not be set in the Master Page's event handlers, because as can be seen in the following trace output, when a page uses a master page, the master page is interpreted as a control of the page and the
Page_Loadevent of this master page is executed during LoadControls() which is executed after the Page'sPage_Loadhandler.I also want to mention the tracing feature here. By enabling tracing, it is possible to see a lot of information about the page's execution process, its children, all the timings, headers etc.:
In the web.config file, under
<system.web>:Or read here for the
<system.webServer>equivalent:https://learn.microsoft.com/en-us/iis/configuration/system.webserver/tracing/