問:DateTime.Parse使用文化EN-US,儘管web.config的設置
在web.config中 在第
system.web
我
<globalization culture="de-ch" uiCulture="de-ch" requestEncoding="UTF-8" responseEncoding="UTF-8"/>
我要的是解析這樣的字符串
"20.03.2012 00:00:00"
爲datetime值
但
DateTime dtAsIs = DateTime.Parse("20.03.2012 00:00:00")
拋出一個異常
遺憾的是隻在TESTSERVER,而不是我的開發系統。 我無權訪問測試服務器,除了將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
到DE-CH在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中的設置。
而不是在每個頁面上設置它,你可以在Global.asax.cs中的''''Begin_Request''中設置,它的工作原理是一樣的。 – 2013-03-18 04:48:55