C# 有没有办法从静态上下文中获取会话对象?

C# 有没有办法从静态上下文中获取会话对象?,c#,asp.net,session,C#,Asp.net,Session,我在一个C#/ASP.NET项目中。我希望能够从静态上下文中获取会话对象(HttpSessionState)的句柄。有什么办法吗?听起来你在寻找:- var sess = HttpContext.Current.Session; 听起来你在寻找:- var sess = HttpContext.Current.Session; 是的,HttpContext上的Current属性是静态的,因此: System.Web.HttpContext.Current.Session 将从静态上下文返回

我在一个C#/ASP.NET项目中。我希望能够从静态上下文中获取会话对象(HttpSessionState)的句柄。有什么办法吗?

听起来你在寻找:-

var sess = HttpContext.Current.Session;

听起来你在寻找:-

var sess = HttpContext.Current.Session;

是的,
HttpContext
上的
Current
属性是静态的,因此:

System.Web.HttpContext.Current.Session

将从静态上下文返回当前会话(但您必须在HTTP上下文中,否则
current
将为空)。

是的,
HttpContext
上的
current
属性是静态的,因此:

System.Web.HttpContext.Current.Session
将从静态上下文返回当前会话(但您必须在HTTP上下文中,否则当前会话将为空)。

请尝试以下操作:

    private static new HttpSessionState Session
    {
        get { return HttpContext.Current.Session; }
    }
然后从另一个静态函数中,您可以将其称为

var myObj = Session[myKey];
就像您在常规非静态代码中所做的那样。

尝试以下方法:

    private static new HttpSessionState Session
    {
        get { return HttpContext.Current.Session; }
    }
然后从另一个静态函数中,您可以将其称为

var myObj = Session[myKey];
就像从常规的非静态代码中一样。