【发布时间】: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