它是安全的使用本地化的一个HttpModule? [英] Is it safe to use an HttpModule for localization?

查看:116
本文介绍了它是安全的使用本地化的一个HttpModule?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在考虑利用一个HttpModule的本地化目的(根据的这篇文章) - 但我很好奇,这是安全的?

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

这里的code,以供参考:

Here's 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;
        }
    }
}

我是IM pression多个线程可能服务的Web请求下。它是安全设置当前/当前UI文化在一个HttpModule这样,让它在服务它推崇为Web请求的生活不管有多少线程参与?

I was under the impression that multiple threads could potentially service a web request. Is it safe to set the Current/Current UI Cultures in an HttpModule like this and have it respected for the life of the web request regardless of how many threads are involved in servicing it?

更新:

我一直用这种方法生产了近一年了,所以我当然可以验证它是绝对安全使用一个HttpModule本地化。

I have been using this method in production for nearly a year now, so I can certainly verify that it is perfectly safe to use an HttpModule for localization.

推荐答案

是的,这应该是罚款。

林pretty确保只有一个线程将服务请求,除非你明确启动另一个,在这种情况下,文化(和其他东西)复制到其他线程。

Im pretty sure that only one thread will service a request, unless you explicitly start another, and in that case, the culture (and other things) are copied to the other thread.

这篇关于它是安全的使用本地化的一个HttpModule?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆