Http模块更改区域性无效

本文关键字:区域性 无效 模块 Http | 更新日期: 2023-09-27 18:27:10

我在项目中使用CultureModule.cs根据变量的值设置区域性信息。以下是的示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization; 
namespace CwizBankApp
{
    public class CultureModule:IHttpModule 
    {
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            context.PostAuthenticateRequest += 
                          new EventHandler(context_PostAuthenticateRequest);
        }
        void context_PostAuthenticateRequest(object sender, EventArgs e)
        {
            CultureInfo currentCulture;
            if (Global.gDateFormat.Trim() == "British")
            {
                currentCulture = new System.Globalization.CultureInfo("en-GB");
            }
            else
            {
                currentCulture = new System.Globalization.CultureInfo("en-US");
            }

            System.Threading.Thread.CurrentThread.CurrentCulture 
                                                             = currentCulture;
        }
    }
}

之后,我在web.config中对其进行如下配置:

<add name="CultureModule" 
               type="CwizBankApp.HttpModules.CultureModule,CwizBankApp"/>

目前我的变量是英国格式,但日期是以美国格式执行的。

我的问题是,我是以正确的方式做这件事,还是仍然缺少一些东西。

Http模块更改区域性无效

格式取决于Thread.CurrentUICulture.

所以你会这么做:

System.Threading.Thread.CurrentThread.CurrentUICulture = currentCulture;

了解如何在msdn上的asp.net中设置Culture。