ACP*_*ACP 58 c# asp.net methods static session-variables
我正在使用带有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;
}
Run Code Online (Sandbox Code Playgroud)
当我编译这个时,我得到错误:
非静态字段,方法或属性'System.Web.UI.Page.Session.get'需要对象引用
jww*_*art 100
HttpContext.Current.Session["..."]
HttpContext.Current得到你当前......好吧,Http Context; 您可以从中访问:会话,请求,响应等
Jon*_*eet 20
如果你没有改变线程,你可以使用HttpContext.Current.Session,如jwwishart所示.
HttpContext.Current返回与线程关联的上下文.显然,这意味着如果您已经启动了新线程,则无法使用它.您可能还需要考虑线程敏捷性 - ASP.NET请求并不总是在整个请求的同一线程上执行.我相信上下文是适当传播的,但是要记住这一点.