Can I use an HttpModule for localization?

I am considering using HttpModule for localization purposes (based on an example in this article ) - but I'm curious, is this safe?

Here is the code for reference:

public class CookieLocalizationModule : IHttpModule { public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } void context_BeginRequest(object sender, EventArgs e) { // eat the cookie (if any) and set the culture if (HttpContext.Current.Request.Cookies["lang"] != null) { HttpCookie cookie = HttpContext.Current.Request.Cookies["lang"]; string lang = cookie.Value; var culture = new System.Globalization.CultureInfo(lang); // is it safe to do this in all situations? Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; } } } 

I got the impression that multiple threads could potentially serve the web request. Is it safe to set current / current UI cultures in an HttpModule like this, and does it respect it throughout the web request no matter how many threads are involved in serving it?

Update:

I have been using this method in production for almost a year now, so I can, of course, make sure that HttpModule is quite safe for localization.

+4
source share
1 answer

Yes, that should be good.

I am sure that only one thread will serve the request, unless you explicitly start another, in which case the culture (and other things) will be copied to another thread.

+2
source

Source: https://habr.com/ru/post/1339116/


All Articles