ASP.NET: How to access Session from handler? [duplicate]
-
21-08-2019 - |
Question
This question already has an answer here:
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?
Solution
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");
}
}
OTHER TIPS
Implement IRequiresSessionState
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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow