2011-10-25 31 views

回答

4

Global.asax文件中,您可以設置當前的文化,即使其Web服務或網頁。

// PreRequestHandlerExecute occurs after initialization of Session 
void Application_PreRequestHandlerExecute(Object Sender, EventArgs e) 
{ 
    // check if session is required for the request 
    // as .css don't require session and accessing session will throw exception 
    if (Context.Handler is IRequiresSessionState 
     || Context.Handler is IReadOnlySessionState) 
    { 
     string culture = "en-US"; 
     if (Session["MyCurrentCulutre"] != null) 
     { 
      culture = Session["MyCurrentCulutre"] as String; 
     } 

     System.Threading.Thread.CurrentThread.CurrentCulture = 
      System.Globalization.CultureInfo.CreateSpecificCulture(culture); 
    } 
} 

你正在改變你的要求,但是在Begin_Request方法Session對象將不可用,你可以在你的Web方法做到這一點。

[WebMethod] 
public static string MyWebMethod() 
{ 
    String culture = Session["MyCurrentCulutre"] as String; 

    System.Threading.Thread.CurrentThread.CurrentCulture = 
     System.Globalization.CultureInfo.CreateSpecificCulture(culture); 

    return "My results"; 
} 
+0

InitializeCulture()是Page Method而不是System.Web.Services.WebService方法。 – rtcardoso

+1

@Waqas Raja,你可以從現實生活中給我一個免費的例子嗎?爲什麼我每個人都想這樣做?讓我們從以色列說im,iis在我們身邊......你能舉個例子嗎? –

+0

想象一下,我保存會話中的首選用戶語言。然後我的JavaScript做了一個web服務調用。在回覆中,我想用用戶首選語言發送用戶消息... – rtcardoso