【问题标题】:How to access a global variable from a WebMethod?如何从 WebMethod 访问全局变量?
【发布时间】:2011-10-11 14:17:15
【问题描述】:

我有以下全局变量:

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

我可以在网络表单的每个过程中使用它。

但是,我需要在同一个 WebForm 中的 WebMethod 中使用它,但它似乎无法识别任何全局变量。所以:

如何从 WebMethod 访问全局变量?

【问题讨论】:

    标签: c# asp.net ajax global-variables webmethod


    【解决方案1】:

    您将值存储在 WebMethod 无法使用的 Viewstate 中,请尝试改用“Session”变量。

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

    【讨论】:

      【解决方案2】:

      ViewState 属性取决于让页面 (.aspx) 回发视图状态,这是存储“变量”的位置。 WebMethod 不包括整页回发(如果有回发的话),因此没有可供读取的视图状态。相反,您可能希望使用会话变量,例如:

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

      会话将变量存储在 Web 服务器的内存中(但与特定的浏览器会话相关)。这有它自己的缺点,例如对工作进程重置、负载平衡考虑等不稳定。

      【讨论】:

      • 我认为您可能需要包含属性<WebMethod(EnableSession:=True)>
      【解决方案3】:

      您不能在 Web 方法中访问非静态属性。如果您的业务规则允许您应该使用静态属性

      【讨论】:

        【解决方案4】:

        是的,你可以。 ' VB .net 示例 _ 公共共享函数 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# 示例: [WebMethod(Description = "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...";
        }
        // •————————————————————————————————————————————————————•
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-05-04
          • 2010-09-22
          • 1970-01-01
          • 2015-03-30
          • 1970-01-01
          • 2012-10-11
          • 1970-01-01
          相关资源
          最近更新 更多