问题:
在 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 中的设置。