在不同的服务器上部署 Web 应用程序时遇到问题。似乎有些不一致 DateTimeFormat
模式,如 ShortDatePattern
, 使用相同的文化 (pt-BR)。
在我的开发机器(安装了 Windows 7,.NET 4,应用程序面向 .NET 3.5)和 Windows Server 2008 R2(应用程序面向 .NET 4)服务器中,ShortDatePattern 是 "dd/MM/yyyy"
- 这是正确的,我猜。
在生产服务器(Windows Server 2003,使用 .NET 3.5)中,它是 "d/M/yyyy"
.它给我带来了很多麻烦。
我可以通过手动设置模式来解决这个问题,但我真的很想避免每次需要输出日期时都这样做。特别是因为这在许多地方(比如我使用 MVC 的 Html.TextBoxFor 的地方)都不是微不足道的,并且需要大量的重写。
如果有一种方法可以在一个地方更改整个 Web 应用程序的模式,那就太好了。我在 Global.asax.cs
中尝试了以下方法文件,没有成功:
CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
有任何想法吗?
谢谢你。
最佳答案
实际上,我试图在 Global.asax 文件中的错误位置使用上面的代码。
通过将代码放入 Application_BeginRequest 方法,我设法覆盖了整个应用程序的 ShortDatePattern:
protected void Application_BeginRequest()
{
CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
}
关于asp.net - 不同机器上相同文化的不同 DateTimeFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5505647/