【发布时间】:2011-02-04 08:27:21
【问题描述】:
我正在使用带有 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”需要对象引用`
【问题讨论】:
-
像这样使用 HttpContext.Current:HttpContext.Current.Session["UserName"].ToString();
标签: c# asp.net methods static session-variables