如何在静态方法中获取会话变量的值?

发布于 2024-08-27 20:55:14 字数 404 浏览 5 评论 0原文

我正在使用带有 jQ​​uery 的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜巴黎 2024-09-03 20:55:14

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

谁的新欢旧爱 2024-09-03 20:55:14

如果您没有更改线程,则可以使用 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.

我乃一代侩神 2024-09-03 20:55:14

试试这个:

HttpContext.Current.Session["UserName"].ToString();

Try this:

HttpContext.Current.Session["UserName"].ToString();
别理我 2024-09-03 20:55:14

您可以通过 HttpContext.Current - 一个静态属性,通过它您可以检索 HttpContext 实例适用于当前的网络请求。这是静态应用程序代码和静态页面方法中的常见模式。

string s = (string)HttpContext.Current.Session["UserName"];

使用相同的技术从用 [WebMethod(EnableSession = true)] 修饰的 ASMX Web 方法中访问 Session,因为虽然此类方法不是静态的,但它们不会继承自Page,因此无法直接访问 Session 属性。

静态代码可以以相同的方式访问应用程序缓存

string var1 = (string)HttpContext.Current.Cache["Var1"];

如果静态代码位于另一个项目中,我们需要引用System.Web.dll。但是,在这种情况下,通常最好避免这种依赖关系,因为如果从 ASP.NET 上下文外部调用代码,HttpContext.Current 将是 null,原因显而易见。相反,我们可以要求 HttpSessionState 作为参数(当然我们仍然需要对 System.Web 的引用)

public static class SomeLibraryClass
{
    public static string SomeLibraryFunction(HttpSessionState session)
    {
       ...
    }
}

[WebMethod]
public static string GetName()
{
    return SomeLibraryClass.SomeLibraryFunction(HttpContext.Current.Session);
}

You can access the current Session via HttpContext.Current - a static property through which you can retrieve the HttpContext instance that applies to the current web request. This is a common pattern in static App Code and static page methods.

string s = (string)HttpContext.Current.Session["UserName"];

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 from Page and thus do not have direct access to a Session property.

Static code can access the Application Cache in the same way:

string var1 = (string)HttpContext.Current.Cache["Var1"];

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 context HttpContext.Current will be null, for obvious reasons. Instead, we can require a HttpSessionState as an argument (we'll still need the reference to System.Web of course):

public static class SomeLibraryClass
{
    public static string SomeLibraryFunction(HttpSessionState session)
    {
       ...
    }
}

Call:

[WebMethod]
public static string GetName()
{
    return SomeLibraryClass.SomeLibraryFunction(HttpContext.Current.Session);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文