2012-03-20 32 views
1

問: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中的設置。

回答

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 

然後,在代碼隱藏,以PageWithCorrectPageCulture

Partial Class whateverpage 
    Inherits PageWithCorrectPageCulture 
    'Inherits System.Web.UI.Page 

對於那些誰只能複製磷酸酶C#替換System.Web.UI.Page:

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

    } 
} 
+0

而不是在每個頁面上設置它,你可以在Global.asax.cs中的''''Begin_Request''中設置,它的工作原理是一樣的。 – 2013-03-18 04:48:55

1

我和你有相同的經歷,似乎web.config中的全球化標籤被忽略了。 但是因爲你總是想在de-ch文化中解析日期,所以我沒有看到僅僅提供文化到DateTime.Parse方法有什麼問題(有些指導說這是最好的辦法)

+0

問題是我沒有時間將它替換到任何地方(「僅」約1000到10,000個事件分佈在Wi-Fi上頁面和模板的範圍...)。現在我嘗試通過繼承自定義頁面並在OnInit之前設置CurrentCulture來修復它。 – 2012-03-20 14:15:25

+0

當我在Date.Parse中設置文化時,它仍然忽略它。但現在,我有解決方案...看到我的答案。 – 2012-03-21 10:10:03

相關問題