views:

1504

answers:

3

i'm trying to store some values in the Session from a Handler page, before i do a redirect to a WebForms page, that will pick up the Session values and pre-fill the WebForm:

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

Except context.Session object is null.

How do i access Session state from a handler?

+17  A: 

Implement the System.Web.SessionState.IRequiresSessionState interface

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

}
JoshBerke
Note: you don't have to actually implement anything, just add the interface to your class. The web-server then sees that you're asking for it, and fills it in.
Ian Boyd
Yes which is still implementing the interface but since it's a marker interface there isn't any code we have to write other then the deriviation of the interface.
JoshBerke
+3  A: 

implement IRequiresSessionState

Tim Hoolihan
+1  A: 

Does implementing iRequiresSessionState resolve this?

What about doing an IHttpModule instead and overriding BeginRequest?

    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(context_BeginRequest);
    }
yodaj007
Yes it does....
Ian Boyd
Does anyone know which is better performance-wise?
Chris Dwyer