如何獲取Page.UICulture
中的static
WebMethod
函數。下面給我一個錯誤:如何在靜態WebMethod中獲取Page.UICulture?
Page.UICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
的錯誤,我得到:
An object reference is required for the non static field,method or property
如何獲取Page.UICulture
中的static
WebMethod
函數。下面給我一個錯誤:如何在靜態WebMethod中獲取Page.UICulture?
Page.UICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
的錯誤,我得到:
An object reference is required for the non static field,method or property
簡單,你不能。
該屬性Page
不適用於static
方法,這是有道理的,因爲在該靜態WebMethod
的上下文中,您沒有頁面。
您可以將相關屬性保存在Session
變量中,並從您的WebMethod
中的會話信息中獲取。請注意,您必須設置[WebMethod(EnableSession=true)]
才能從WebMethod
獲取會話信息。
請嘗試下面的代碼。我不嘗試,但它可能對你有幫助。
public static Page obj;
protected void Page_Load(object sender, EventArgs e)
{
obj = this.Page;
}
public static void function1()
{
obj.UICulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
}
可能工作,但它是超級討厭的。 – DavidG 2015-02-09 11:27:06
ASP.NET中的'static'變量非常危險,因爲它們在所有會話中共享,所以你真的不想使用'static'。 – 2015-02-09 11:28:08
@DavidG:...而且非常危險。 – 2015-02-09 11:28:32
2 Page'UICulture = System.Threading.Thread.CurrentThread.CurrentCulture;'導致編譯錯誤或運行時異常? – 2015-02-09 11:42:51
@JenishRabadiya:通常編譯時。它在代碼中,而不是在標記中。這將使編譯時間被檢查。 – 2015-02-09 11:44:06