ASP.NET:如何从处理程序访问Session?

时间:2009-06-29 14:18:11

标签: asp.net session session-state handler ihttphandler

我是trying to store some values in the Session from a Handler page,在我重定向到WebForms页面之前,它将获取 Session 值并预先填充WebForm:

public class Handler : IHttpHandler
{
   public void ProcessRequest(HttpContext context)
   {
      ...
      context.Session["StackOverflow"] = "overflowing";
      context.Response.Redirect("~/AnotherPage.aspx");
      ...
   }
   ...
 }

除了context.Session对象为空。

如何从处理程序访问会话状态?

3 个答案:

答案 0 :(得分:106)

实施System.Web.SessionState.IRequiresSessionState界面

public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{   
  public void ProcessRequest(HttpContext context)  
  {      
    context.Session["StackOverflow"] = "overflowing";      
    context.Response.Redirect("~/AnotherPage.aspx");      
  }

}

答案 1 :(得分:10)

实施IRequiresSessionState

答案 2 :(得分:7)

实施iRequiresSessionState会解决这个问题吗?

如果改为使用IHttpModule并覆盖BeginRequest呢?

    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(context_BeginRequest);
    }