How to access a global variable from a WebMethod?
I have the following global variable:
private ArrayList listSelectedUnavailables
{
get
{
return (ArrayList)ViewState["listSelectedUnavailables"];
开发者_如何学C }
set
{
ViewState["listSelectedUnavailables"] = value;
}
}
I can work with it in every single procedure of the webform.
However, I need to use it in a WebMethod that I have in the same WebForm, but it seems not to identify any of the global variables. So:
How can I access a global variable from a WebMethod?
You are storing the value in the Viewstate, which will not be available to the WebMethod, try using a 'Session' variable instead.
private ArrayList listSelectedUnavailables
{
get
{
return (ArrayList)Session["listSelectedUnavailables"];
}
set
{
Session["listSelectedUnavailables"] = value;
}
}
A ViewState
property depends on having a page (.aspx) post back a view state, that's where your "variable" is stored. A WebMethod
does not include the full page postback (if any post back at all) and so there is no view state for it to read from. Instead you may want to use a session variable like:
private ArrayList listSelectedUnavailables
{
get
{
return (ArrayList)Session["listSelectedUnavailables"];
}
set
{
Session["listSelectedUnavailables"] = value;
}
}
The session stores the variable in the web server's memory (but related to a specific browser session). This has it's own draw-backs, such as being volatile to a worker process reset, load balancing cosiderations, etc.
you cannot access a non static property within a web method. if your business rule allows that you should use a static property
Yes you can. ' VB .net sample _ Public Shared Function LoadController(ByVal serial As String) As String
' 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# sample: [WebMethod(Description = "Proiecto", CacheDuration = 0)] public static string LoadController(string serial) {
// 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...";
}
// •————————————————————————————————————————————————————•
}
精彩评论