如何在静态方法中获取会话变量的值?
我正在使用带有 jQuery 的 ASP.NET 页面方法...如何在 C# 中的静态方法中获取会话变量的值?
protected void Page_Load(object sender, EventArgs e)
{
Session["UserName"] = "Pandiya";
}
[WebMethod]
public static string GetName()
{
string s = Session["UserName"].ToString();
return s;
}
当我编译这个时,我收到错误:
非静态字段、方法或属性“System.Web.UI.Page.Session.get”需要对象引用`
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#?
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'`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
HttpContext.Current.Session["..."]
HttpContext.Current
获取当前的...好吧,Http Context;您可以从中访问:会话、请求、响应等HttpContext.Current.Session["..."]
HttpContext.Current
gets you the current ... well, Http Context; from which you can access: Session, Request, Response etc如果您没有更改线程,则可以使用
HttpContext.Current.Session
,如 jwwishart 所示。HttpContext.Current
返回与线程关联的上下文。显然,这意味着如果您启动了一个新线程,则无法使用它。您可能还需要考虑线程敏捷性 - ASP.NET 请求并不总是在整个请求的同一线程上执行。我相信上下文得到了适当的传播,但需要牢记这一点。If you haven't changed thread, you can use
HttpContext.Current.Session
, as indicated by 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.试试这个:
Try this:
您可以通过
HttpContext.Current
- 一个静态属性,通过它您可以检索HttpContext
实例适用于当前的网络请求。这是静态应用程序代码和静态页面方法中的常见模式。使用相同的技术从用
[WebMethod(EnableSession = true)]
修饰的 ASMX Web 方法中访问Session
,因为虽然此类方法不是静态的,但它们不会继承自Page
,因此无法直接访问Session
属性。静态代码可以以相同的方式访问应用程序缓存:
如果静态代码位于另一个项目中,我们需要引用
System.Web.dll
。但是,在这种情况下,通常最好避免这种依赖关系,因为如果从 ASP.NET 上下文外部调用代码,HttpContext.Current
将是null
,原因显而易见。相反,我们可以要求HttpSessionState
作为参数(当然我们仍然需要对System.Web
的引用):
You can access the current
Session
viaHttpContext.Current
- a static property through which you can retrieve theHttpContext
instance that applies to the current web request. This is a common pattern in static App Code and static page methods.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 fromPage
and thus do not have direct access to aSession
property.Static code can access the Application Cache in the same way:
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 contextHttpContext.Current
will benull
, for obvious reasons. Instead, we can require aHttpSessionState
as an argument (we'll still need the reference toSystem.Web
of course):Call: