1

问题:

在 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")

抛出异常

不幸的是,只在测试服务器上,不在我的开发系统上。我无权访问测试服务器,除了将 webapp 复制到 Windows 共享中。

我可以像这样重现异常:

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 的一开始就 de-ch

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 中的设置。

4

2 回答 2

1

我和你有同样的经历,似乎web.config中的全球化标签被忽略了。但是由于您总是想在 de-ch 文化中解析日期,所以我看不出仅将文化提供给 DateTime.Parse 方法有什么问题(一些指导方针说这是最好的做法)

于 2012-03-20T13:44:07.167 回答
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))

注意带假的重载,这是必要的,否则它实际上不起作用。

这是我的解决方案:

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

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

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);
        }

    }
}
于 2012-03-21T09:18:26.673 回答