如何改变当前区域性的数据格式,使其适用于整个Web应用程序 [英] How to change the data format for the current culture so that it applies to the entire web application

查看:223
本文介绍了如何改变当前区域性的数据格式,使其适用于整个Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置的日期格式为 YYYY-MM-DD 我的整个Web应用程序。我可以通过创建 CultureAndRegionInfoBuilder ,然后注册一个对象做到这一点。但由于我的应用程序在托管环境,我不能运行的可执行文件来注册的文化。

我需要一种方法来在的Application_Start内做到这一点,使其适用于整个Web应用程序。

我尝试使用的 Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern 的更改日期格式,但它似乎并没有传播改变整个应用程序。

总之,我需要一种方法来从在Web应用程序本身编程改变整个Web应用程序的当前区域性设置。

编辑:我试着@doitgood的建议下,却得到了异常'System.InvalidOperationException:实例是只读的。,而智能感知显示属性作为获取或设置<。 / p>

无效的Application_BeginRequest(对象发件人,EventArgs的发送)
    {
        Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern =YYYY-MM-DD;结果
    }

要做出改变全局我试着在的Application_Start下列应用:
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern =YYYY-MM-DD;

我得到相同的异常实例是只读的。


解决方案

由Sebeto的答案是正确的。我有同样的问题,我只是想它。以为我会添加一些更多的解释。当前区域性的属性确实是只读的,一旦目前的文化已定。因此,必须建立一个新的CultureInfo对象(最简单的方法是通过克隆现有的文化)。然后,您可以用自己交换当前线程文化对象之前调整文化信息对象的属性。

  CultureInfo的newCultureDefinition =(CultureInfo的)Thread.CurrentThread.CurrentCulture.Clone();
//使百分比N%,而不是N%的ToString(P),默认英语文化在asp.net中添加一个空格
newCultureDefinition.NumberFormat.PercentPositivePattern = 1;
newCultureDefinition.DateTimeFormat.ShortDatePattern =YYYY-MM-DD;
Thread.CurrentThread.CurrentCulture = newCultureDefinition;

还有问题的其中的把code键更改当前线程的文化。

根据微软的例子,请参见 http://support.microsoft.com/kb/306162你甚至可以改变的Page_Load当前文化。因为你可能有code,需要的Page_Load之前您的格式,我已经把我的变化,我的基本页面的OnInit。

 保护覆盖无效的OnInit(EventArgs的发送)
{
        initGlobalCulturalFormattingChanges();
}

和加盖其关闭:

 私人无效initGlobalCulturalFormattingChanges()
{    CultureInfo的newCultureDefinition =(CultureInfo的)Thread.CurrentThread.CurrentCulture.Clone();
    //做,而不是N%百分比N%(的ToString(P0),默认情况下,在asp.net中添加一个空格
    newCultureDefinition.NumberFormat.PercentPositivePattern = 1;
    newCultureDefinition.DateTimeFormat.ShortDatePattern =YYYY-MM-DD;
    Thread.CurrentThread.CurrentCulture = newCultureDefinition;}

是不是换出当前线程的文化与每个请求每隔一个页面的init最好的主意?我没有足够的数据来权衡上,但它似乎就ok了工作。)

I would like to set the date format to 'YYYY-MM-DD' for my entire web application. I can do this by creating an object of CultureAndRegionInfoBuilder and then registering it. But since my application is on a hosted environment, I cannot run executables to register the culture.

I need a way to do this within the Application_Start so that it applies to the entire web app.

I tried changing the date format using Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern, but it does not seem to propagate the change to the entire application.

In short, I need a way to change current culture settings of the entire web application programatically from with in the web app itself.

Edit: I tried the following on suggestion of @doitgood, but got exception 'System.InvalidOperationException: Instance is read-only.', while the intellisense shows the property as Get or Set.

void Application_BeginRequest(object sender, EventArgs e) { Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
}

To make the change global to the application I tried the following in Application_Start: System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";

I get the same exception 'Instance is read-only'.

解决方案

The answer by Sebeto is correct. I had the same problem and I just tried it. Thought I'd add some more explanation. The current culture's properties are indeed read-only once the current culture has been set. You must therefore construct a new CultureInfo object (easiest way is by cloning the existing culture). Then, you can tweak the culture info object's properties before swapping the current threads culture object with your own.

CultureInfo newCultureDefinition = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
//make percentages n% instead of n % .ToString("p") adds a space by default english culture in asp.net
newCultureDefinition.NumberFormat.PercentPositivePattern = 1;
newCultureDefinition.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
Thread.CurrentThread.CurrentCulture = newCultureDefinition;

There is also the question of where to put the code to change the current thread's culture.

According to Microsoft's example, see http://support.microsoft.com/kb/306162 you can even change the current culture on Page_Load. Since you might have code that needs your formatting before Page_Load, I have put my change in my base page OnInit.

protected override void OnInit(EventArgs e)
{
        initGlobalCulturalFormattingChanges();
}

and to cap it off:

private void initGlobalCulturalFormattingChanges()
{

    CultureInfo newCultureDefinition = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
    //make percentages n% instead of n % (.ToString("p0") adds a space by default in asp.net
    newCultureDefinition.NumberFormat.PercentPositivePattern = 1;
    newCultureDefinition.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
    Thread.CurrentThread.CurrentCulture = newCultureDefinition;

}

Is it the best idea to swap out the current Thread's culture with every request one every page's init? I don't have enough data to weigh in on that but it seems to work ok :).

这篇关于如何改变当前区域性的数据格式,使其适用于整个Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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