使用culture en-us的DateTime.Parse,尽管web.config设置

时间:2012-03-20 13:29:59

标签: asp.net datetime web-config datetime-format currentculture

问题:

web.config中的

部分
system.web

我有

<globalization culture="de-ch" uiCulture="de-ch" requestEncoding="UTF-8" responseEncoding="UTF-8"/>

我想要的是解析像这样的字符串

"20.03.2012 00:00:00"

到日期时间值

但是

DateTime dtAsIs = DateTime.Parse("20.03.2012 00:00:00")

抛出异常

不幸的是只在testserver上,而不是在我的开发系统上。 除了将webapp复制到Windows共享中之外,我无权访问testserver。

我可以像这样重现这个例外:

DateTime dtThrowsException = DateTime.Parse("20.03.2012 00:00:00",new System.Globalization.CultureInfo("en-us"));

虽然它可以正常工作:

DateTime dtWorks = DateTime.Parse("20.03.2012 00:00:00",new System.Globalization.CultureInfo("de-ch"));

我检查了ASP页面,并且在asp页面中没有设置文化

(我的意思是:

<% @Page Culture="fr-FR" Language="C#" %>

如果我设置

System.Threading.Thread.CurrentThread.CurrentCulture

System.Threading.Thread.CurrentThread.CurrentUICulture

在Page_Load开头就像这样去除

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-ch");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-ch");

然后它运作正常。

浏览器语言设置为“de-ch”,我检查了它。

有人可以告诉我为什么线程文化被设置为英语吗?

我的意思是显而易见的原因是服务器操作系统是英文的,但我无法更改那个,只能在web.config中进行设置。

2 个答案:

答案 0 :(得分:1)

我和你有相同的经历,似乎只是忽略了web.config中的全球化标签。 但是因为你总是想要在de-ch文化中解析日期,所以我没有看到将文化提供给DateTime.Parse方法有什么问题(一些指导方针说这是最好的事情)

答案 1 :(得分:1)

问题似乎是,即使您明确指定文档,ASP.NET也会覆盖文化。 (像

DateTime.Parse("Whatever", New System.Globalization.CultureInfo("de-ch"))

需要强制覆盖它

 New System.Globalization.CultureInfo("de-ch", False)




因此,为了使其可配置并尽可能少地进行更改,您需要使用

从web.config获取文化。
System.Globalization.CultureInfo.CurrentCulture.Name

然后用

强制设置它
 DateTime.Parse("Whatever", New System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, False))

注意带有false的重载,这是必要的,否则它不会真正起作用。

这是我的解决方案:

Namespace ASP.NET.Sucks
    Public Class PageWithCorrectPageCulture
        Inherits Web.UI.Page

        Protected Sub New()
            System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, False)
            System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, False)
        End Sub

    End Class
End Namespace

然后,在代码隐藏中,用PageWithCorrectPageCulture替换System.Web.UI.Page

Partial Class whateverpage
    Inherits PageWithCorrectPageCulture
    'Inherits System.Web.UI.Page

对于那些只能复制C#的人来说:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace ASP.NET.Sucks
{
    public class PageWithCorrectPageCulture : Web.UI.Page
    {

        protected PageWithCorrectPageCulture()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, false);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, false);
        }

    }
}