2015-02-09 50 views
3

如何获取Page.UICulture中的staticWebMethod函数。下面给我一个错误:如何在静态WebMethod中获取Page.UICulture?

Page.UICulture = System.Threading.Thread.CurrentThread.CurrentCulture; 

的错误,我得到:

An object reference is required for the non static field,method or property

回答

3

简单,你不能。

该属性Page不适用于static方法,这是有道理的,因为在该静态WebMethod的上下文中,您没有页面。

您可以将相关属性保存在Session变量中,并从您的WebMethod中的会话信息中获取。请注意,您必须设置[WebMethod(EnableSession=true)]才能从WebMethod获取会话信息。

+0

2 Page'UICulture = System.Threading.Thread.CurrentThread.CurrentCulture;'导致编译错误或运行时异常? – 2015-02-09 11:42:51

+0

@JenishRabadiya:通常编译时。它在代码中,而不是在标记中。这将使编译时间被检查。 – 2015-02-09 11:44:06

-2

请尝试下面的代码。我不尝试,但它可能对你有帮助。

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(); 
    } 
+0

可能工作,但它是超级讨厌的。 – DavidG 2015-02-09 11:27:06

+1

ASP.NET中的'static'变量非常危险,因为它们在所有会话中共享,所以你真的不想使用'static'。 – 2015-02-09 11:28:08

+0

@DavidG:...而且非常危险。 – 2015-02-09 11:28:32