如何在静态方法中获取会话变量的值? [英] How can I get the value of a session variable inside a static method?

查看:22
本文介绍了如何在静态方法中获取会话变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 jQuery 中使用 ASP.NET 页面方法......如何在 C# 中的静态方法中获取会话变量的值?

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;
}

编译时出现错误:

非静态字段、方法或属性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

这篇关于如何在静态方法中获取会话变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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