如何获取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