2010-01-28 80 views
31

我想在Asp.Net ashx处理程序中访问会话变量,如下所示。Asp.Net会话在ashx文件中为空

public void ProcessRequest (HttpContext context) { 
     context.Session["VariableName"] = Id; 
    } 

context.Session总是上述方法的内部。如何访问ashx文件中的会话对象?

回答

94

您必须“执行”IRequiresSessionStateIReadOnlySessionState,前者提供完全访问会话,后者提供只读访问权限。

我在这里引用“实施”,因为这两个是所谓的“marker interfaces”,这意味着他们没有成员。

+2

的感谢!也需要这个 – Michel 2011-03-23 09:08:28

+3

有一个upvote好先生。我唯一的遗憾是我只能给一个。 – CountMurphy 2011-10-17 20:46:34

+0

太棒了!而关于“标记界面”,如果他们将它作为注释,会不会更好? – everton 2011-12-22 12:54:21

2

在VB中,实现由安东(IRequiresSessionState或IReadOnlySessionState)这样提到的接口:

Public Class MyAshxFile 

    Implements System.Web.IHttpHandler 
    Implements System.Web.SessionState.IRequiresSessionState ''need this for session variables 
    Implements System.Web.SessionState.IReadOnlySessionState ''need this for session variables