I created the current solution some time ago with the help of a tutorial. At the time it looked like a best solution. I created the solution by adding Application event Listener AcquireRequestState into global.asax. Here I than checked , through simple condition, if there is a Session running. If there was one, I adjusted according to it. (see code below)
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (context != null && context.Session != null && Session[sessionNameCulture] != null)
{
string culture = Session[sessionNameCulture].ToString();
try
{
culture = culture.ToLower();
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); ;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); ;
}
catch (Exception ex)
{
ErrorHandler.HandleError("Culture info setup failure.", ex);
}
}
}
Back to my question. I need to, when there is no existing Session, to set culture according to the settings of the browser. Unfortunately, the event is being called approx. three times and during one of the calls:
if(Session[sessionNameCulture] == null)
It ends up with exception. I found an alternative solution, which would consist of moving the logic into BaseController, where I could catch the Page_load event or use something like this.
Before I start redoing the whole project, I would like to make sure there isn’t a way to solve this through catching Application events. I have in mind something like:
protected void Application_AcquireRequestState(object sender, EventArgs e) {
HttpContext context = HttpContext.Current; if(Session[sessionNameCulture] == null)
Session[SessionNameCulture] = new System.Globalization.CultureInfo(Request.UserLanguages[0]);
if (context != null && context.Session != null && Session[sessionNameCulture] != null)
{
... }
Also, it might be a problem that this is supposed to work for a not logged in user.