I am using ASP.NET page methods with jQuery.... How do I get the value of a session variable inside a static method in C#?
我用ASP。与jQuery ....网络页面的方法如何在c#中的静态方法中获取会话变量的值?
protected void Page_Load(object sender, EventArgs e)
{
Session["UserName"] = "Pandiya";
}
[WebMethod]
public static string GetName()
{
string s = Session["UserName"].ToString();
return s;
}
When I compile this I get the error:
当我编译这个时,我得到了错误:
An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'`
非静态字段、方法或属性'System.Web.UI.Page.Session.get'需要对象引用。
4 个解决方案
#1
87
HttpContext.Current.Session["..."]
HttpContext.Current.Session(“…”)
HttpContext.Current
gets you the current ... well, Http Context; from which you can access: Session, Request, Response etc
HttpContext。电流使你得到电流…Http上下文;您可以从其中访问:会话、请求、响应等
#2
16
If you haven't changed thread, you can use HttpContext.Current.Session
, as indicated by jwwishart.
如果没有改变线程,可以使用HttpContext.Current。会话,如jwwishart所示。
HttpContext.Current
returns the context associated with the thread. Obviously this means you can't use it if you've started a new thread, for example. You may also need to consider thread agility - ASP.NET requests don't always execute on the same thread for the whole of the request. I believe that the context is propagated appropriately, but it's something to bear in mind.
HttpContext。当前返回与线程关联的上下文。显然,这意味着如果启动了一个新线程,就不能使用它。您可能还需要考虑线程敏捷性- ASP。NET请求并不总是在同一线程上执行整个请求。我相信上下文是适当传播的,但这是需要记住的。
#3
2
Try this:
试试这个:
HttpContext.Current.Session["UserName"].ToString();
#4
1
You can access the current Session
via HttpContext.Current
- a static property through which you can retrieve the HttpContext
instance that applies to the current web request. This is a common pattern in static App Code and static page methods.
您可以通过HttpContext访问当前会话。Current—一个静态属性,您可以通过它检索适用于当前web请求的HttpContext实例。这是静态应用程序代码和静态页面方法中的常见模式。
string s = (string)HttpContext.Current.Session["UserName"];
The same technique is used to access the Session
from within ASMX web methods decorated with [WebMethod(EnableSession = true)]
because whilst such methods are not static they do not inherit from Page
and thus do not have direct access to a Session
property.
同样的技术用于从装饰有[WebMethod(EnableSession = true)]的ASMX web方法中访问会话,因为这些方法不是静态的,它们不是从页面继承的,因此不能直接访问会话属性。
Static code can access the Application Cache in the same way:
静态代码可以以相同的方式访问应用程序缓存:
string var1 = (string)HttpContext.Current.Cache["Var1"];
If the static code is inside another project we need to reference System.Web.dll
. However, in this case it is generally best to avoid such a dependency because if the code is called from outside of an ASP.NET context HttpContext.Current
will be null
, for obvious reasons. Instead, we can require a HttpSessionState
as an argument (we'll still need the reference to System.Web
of course):
如果静态代码在另一个项目中,我们需要引用System.Web.dll。但是,在这种情况下,最好避免这种依赖关系,因为如果代码是从ASP外部调用的。HttpContext净上下文。当前将是空的,原因很明显。相反,我们可以要求HttpSessionState作为参数(我们仍然需要对系统的引用)。当然网络):
public static class SomeLibraryClass
{
public static string SomeLibraryFunction(HttpSessionState session)
{
...
}
}
Call:
电话:
[WebMethod]
public static string GetName()
{
return SomeLibraryClass.SomeLibraryFunction(HttpContext.Current.Session);
}
#1
87
HttpContext.Current.Session["..."]
HttpContext.Current.Session(“…”)
HttpContext.Current
gets you the current ... well, Http Context; from which you can access: Session, Request, Response etc
HttpContext。电流使你得到电流…Http上下文;您可以从其中访问:会话、请求、响应等
#2
16
If you haven't changed thread, you can use HttpContext.Current.Session
, as indicated by jwwishart.
如果没有改变线程,可以使用HttpContext.Current。会话,如jwwishart所示。
HttpContext.Current
returns the context associated with the thread. Obviously this means you can't use it if you've started a new thread, for example. You may also need to consider thread agility - ASP.NET requests don't always execute on the same thread for the whole of the request. I believe that the context is propagated appropriately, but it's something to bear in mind.
HttpContext。当前返回与线程关联的上下文。显然,这意味着如果启动了一个新线程,就不能使用它。您可能还需要考虑线程敏捷性- ASP。NET请求并不总是在同一线程上执行整个请求。我相信上下文是适当传播的,但这是需要记住的。
#3
2
Try this:
试试这个:
HttpContext.Current.Session["UserName"].ToString();
#4
1
You can access the current Session
via HttpContext.Current
- a static property through which you can retrieve the HttpContext
instance that applies to the current web request. This is a common pattern in static App Code and static page methods.
您可以通过HttpContext访问当前会话。Current—一个静态属性,您可以通过它检索适用于当前web请求的HttpContext实例。这是静态应用程序代码和静态页面方法中的常见模式。
string s = (string)HttpContext.Current.Session["UserName"];
The same technique is used to access the Session
from within ASMX web methods decorated with [WebMethod(EnableSession = true)]
because whilst such methods are not static they do not inherit from Page
and thus do not have direct access to a Session
property.
同样的技术用于从装饰有[WebMethod(EnableSession = true)]的ASMX web方法中访问会话,因为这些方法不是静态的,它们不是从页面继承的,因此不能直接访问会话属性。
Static code can access the Application Cache in the same way:
静态代码可以以相同的方式访问应用程序缓存:
string var1 = (string)HttpContext.Current.Cache["Var1"];
If the static code is inside another project we need to reference System.Web.dll
. However, in this case it is generally best to avoid such a dependency because if the code is called from outside of an ASP.NET context HttpContext.Current
will be null
, for obvious reasons. Instead, we can require a HttpSessionState
as an argument (we'll still need the reference to System.Web
of course):
如果静态代码在另一个项目中,我们需要引用System.Web.dll。但是,在这种情况下,最好避免这种依赖关系,因为如果代码是从ASP外部调用的。HttpContext净上下文。当前将是空的,原因很明显。相反,我们可以要求HttpSessionState作为参数(我们仍然需要对系统的引用)。当然网络):
public static class SomeLibraryClass
{
public static string SomeLibraryFunction(HttpSessionState session)
{
...
}
}
Call:
电话:
[WebMethod]
public static string GetName()
{
return SomeLibraryClass.SomeLibraryFunction(HttpContext.Current.Session);
}