5
誰能告訴我如何動態初始化線程文化在一個asp.net web服務調用?如何動態初始化ASP.NET WebService調用中的文化?
在我的aspx頁面中,我有一個基本頁面,我重寫了InitializeCulture()方法。
請注意,所選語言的值將保存在會話狀態中。
誰能告訴我如何動態初始化線程文化在一個asp.net web服務調用?如何動態初始化ASP.NET WebService調用中的文化?
在我的aspx頁面中,我有一個基本頁面,我重寫了InitializeCulture()方法。
請注意,所選語言的值將保存在會話狀態中。
在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";
}
InitializeCulture()是Page Method而不是System.Web.Services.WebService方法。 – rtcardoso
@Waqas Raja,你可以從現實生活中給我一個免費的例子嗎?爲什麼我每個人都想這樣做?讓我們從以色列說im,iis在我們身邊......你能舉個例子嗎? –
想象一下,我保存會話中的首選用戶語言。然後我的JavaScript做了一個web服務調用。在回覆中,我想用用戶首選語言發送用戶消息... – rtcardoso