2011-10-11 103 views
1

我有以下全局變量:如何從WebMethod訪問全局變量?

private ArrayList listSelectedUnavailables 
    { 
     get 
     { 
      return (ArrayList)ViewState["listSelectedUnavailables"]; 
     } 
     set 
     { 
      ViewState["listSelectedUnavailables"] = value; 
     } 
    } 

我可以在網頁表單的每一個程序使用它。

但是,我需要在WebMethod中使用它,但是它似乎沒有標識任何全局變量。因此:

如何從WebMethod訪問全局變量?

回答

2

A ViewState屬性取決於有一個頁面(.aspx)回發視圖狀態,這是您的「變量」存儲的位置。 A WebMethod不包括完整頁面回發(如果有回發的話),因此沒有視圖狀態供它讀取。相反,你可能需要使用一個會話變量,如:

private ArrayList listSelectedUnavailables 
    { 
     get 
     { 
      return (ArrayList)Session["listSelectedUnavailables"]; 
     } 
     set 
     { 
      Session["listSelectedUnavailables"] = value; 
     } 
    } 

會話存儲在Web服務器的內存中的變量(但是涉及到一個特定的瀏覽器會話)。這有它自己的缺點,比如對於工作進程重置,負載均衡等等易失性等。

+1

我想你可能需要包括屬性'<的WebMethod(ENAB leSession:= TRUE)>' –

3

您正在將值存儲在ViewState中,WebMethod不可用,請嘗試使用'會話「變量。

private ArrayList listSelectedUnavailables 
    { 
     get 
     { 
      return (ArrayList)Session["listSelectedUnavailables"]; 
     } 
     set 
     { 
      Session["listSelectedUnavailables"] = value; 
     } 
    } 
2

您無法訪問Web方法中的非靜態屬性。如果您的業務規則允許您使用靜態屬性

0

是的,您可以。 'VB .NET樣品 _ 公共共享功能LoadController(BYVAL串行作爲字符串)作爲字符串

' sample 1: server object 
    ' •————————————————————————————————————————————————————• 
    ' Crear un objeto server, porque desde un webmethod no se puede acceder directamente.... 
    ' •————————————————————————————————————————————————————• 
    Dim objServer As System.Web.HttpServerUtility 
    objServer = HttpContext.Current.Server 
    Dim lAplicacion As New Aplicacion(objServer.MapPath("~")) 
    Return objServer.MapPath("~") ' ---> P:\Projects\WebApplicationServer\WebApplication\ 
    ' •————————————————————————————————————————————————————• 


    ' sample 2: local variable 
    ' •————————————————————————————————————————————————————• 
    ' Acceder a variables de sesion 
    ' •————————————————————————————————————————————————————• 
    ' Crear un objeto Session (visible solo al uaurio actual), porque desde un webmethod no se puede acceder directamente.... 
    ' Crear la variable = Session("objSession") = "Esto es una variable de sesion" 
    Dim objSesion As System.Web.SessionState.HttpSessionState 
    objSesion = HttpContext.Current.Session 

    If objSesion.Item("objSession") Is Nothing Then 
     Return "No existe la variable local" 
    Else 
     Return objSesion("objSession").ToString 
    End If 
    ' •————————————————————————————————————————————————————• 


    ' sample 3: global variable 
    ' •————————————————————————————————————————————————————• 
    ' Acceder a variables de aplicacion 
    ' •————————————————————————————————————————————————————• 
    ' Crear un objeto Aplicacion (visible a todos los visitantes) , porque desde un webmethod no se puede acceder directamente.... 
    ' Crear la variable = Application("objAplicacion") = "Esto es una variable global..." 
    Dim objAplicacion As System.Web.HttpApplicationState 
    objAplicacion = HttpContext.Current.Application 


    If (Not objAplicacion("objAplicacion") Is Nothing) Then 
     Return objAplicacion("objAplicacion").ToString 
    Else 
     Return " No existe la variable global..." 
    End If 
    ' •————————————————————————————————————————————————————• 
End Function 

// C#樣品: [的WebMethod(描述= 「Proiecto」,CacheDuration = 0)] 公共靜態字符串LoadController(字符串序列) {

// sample 1: server object 
// •————————————————————————————————————————————————————• 
// Crear un objeto server, porque desde un webmethod no se puede acceder directamente.... 
// •————————————————————————————————————————————————————• 
System.Web.HttpServerUtility objServer = default(System.Web.HttpServerUtility); 
objServer = HttpContext.Current.Server; 
Aplicacion lAplicacion = new Aplicacion(objServer.MapPath("~")); 
return objServer.MapPath("~"); 
// ---> P:\Projects\WebApplicationServer\WebApplication\ 
// •————————————————————————————————————————————————————• 


// sample 2: local variable 
// •————————————————————————————————————————————————————• 
// Acceder a variables de sesion 
// •————————————————————————————————————————————————————• 
// Crear un objeto Session (visible solo al uaurio actual), porque desde un webmethod no se puede acceder directamente.... 
// Crear la variable = Session["objSession"] = "Esto es una variable de sesion" 
System.Web.SessionState.HttpSessionState objSesion = default(System.Web.SessionState.HttpSessionState); 
objSesion = HttpContext.Current.Session; 

if (objSesion.Item("objSession") == null) { 
    return "No existe la variable local"; 
} else { 
    return objSesion("objSession").ToString; 
} 
// •————————————————————————————————————————————————————• 


// sample 3: global variable 
// •————————————————————————————————————————————————————• 
// Acceder a variables de aplicacion 
// •————————————————————————————————————————————————————• 
// Crear un objeto Aplicacion (visible a todos los visitantes) , porque desde un webmethod no se puede acceder directamente.... 
// Crear la variable = Application["objAplicacion"] = "Esto es una variable global..." 
System.Web.HttpApplicationState objAplicacion = default(System.Web.HttpApplicationState); 
objAplicacion = HttpContext.Current.Application; 


if (((objAplicacion("objAplicacion") != null))) { 
    return objAplicacion("objAplicacion").ToString; 
} else { 
    return " No existe la variable global..."; 
} 
// •————————————————————————————————————————————————————• 

}