Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: How to access Session from handler? [duplicate]

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?

like image 402
Ian Boyd Avatar asked Jun 29 '09 14:06

Ian Boyd


2 Answers

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");      
  }

}
like image 169
JoshBerke Avatar answered Nov 11 '22 22:11

JoshBerke


Implement IRequiresSessionState

like image 10
Tim Hoolihan Avatar answered Nov 11 '22 23:11

Tim Hoolihan