【问题标题】:DateTime.Parse using culture en-us, despite web.config settingDateTime.Parse 使用文化 en-us,尽管 web.config 设置
【发布时间】:2012-03-20 13:29:59
【问题描述】:

问题:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 问题是我没有时间在任何地方替换它(“仅”大约 1000 到 10'000 次出现分布在各种页面和模板中......)。现在我尝试通过从自定义页面继承并在 OnInit 之前设置 CurrentCulture 来修复它。
    • 当我在 Date.Parse 中设置文化时它仍然忽略它。但是现在,我有了解决方案……看我的答案。
    【解决方案2】:

    问题似乎是 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
    

    然后,在代码隐藏中,将 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);
            }
    
        }
    }
    

    【讨论】:

    • 您可以在Global.asax.cs 中的Begin_Request 中设置,而不是在每个页面上设置它,它的工作原理是一样的。
    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多