我怎样才能得到一个session变量的静态方法里面的价值? [英] How can I get the value of a session variable inside a static method?

查看:306
本文介绍了我怎样才能得到一个session变量的静态方法里面的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net pagemethods与jQuery ....如何获得静态方法中的会话变量的值在C#?

I am using asp.net pagemethods with jQuery.... How to get the value of a session variable inside 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:

这是对象引用是必需的非静态字段,方法或属性System.Web.UI.Page.Session.get'`

An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'`

任何建议或任何其他?

推荐答案

HttpContext.Current.Session [...]

HttpContext.Current 让你当前的......嗯,HTTP上下文;从中可以访问:会话,请求,响应等等

HttpContext.Current gets you the current ... well, Http Context; from which you can access: Session, Request, Response etc

这篇关于我怎样才能得到一个session变量的静态方法里面的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆