我正在考虑使用 HttpModule 进行本地化(基于中的示例)本文 http://weblogs.manas.com.ar/smedina/2008/12/17/internationalization-in-aspnet-mvc/) - 但我很好奇,这安全吗?
这是代码,供参考:
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;
}
}
}
我的印象是多个线程有可能为网络请求提供服务。像这样在 HttpModule 中设置当前/当前 UI 文化是否安全,并在 Web 请求的生命周期内尊重它,无论有多少线程参与服务它?
Update:
我已经在生产中使用这种方法近一年了,所以我当然可以验证使用 HttpModule 进行本地化是完全安全的。
是的,这应该没问题。
我非常确定只有一个线程将为请求提供服务,除非您显式启动另一个线程,在这种情况下,区域性(和其他内容)将被复制到另一个线程。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)