I am trying to access the Session variable in Asp.Net ashx handler as shown below.
public void ProcessRequest (HttpContext context) {
context.Session["VariableName"] = Id;
}
But the context.Session is always Null inside the above method. How do I access Session objects in ashx file?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You have to “implement” either IRequiresSessionState or IReadOnlySessionState, with former providing full access to session, and the latter providing read-only access.
I’m quoting “implement” here because these two are so-called “marker interfaces“, which means they have no members.
Method 2
In VB, implement the interfaces mentioned by Anton (IRequiresSessionState or IReadOnlySessionState) like this:
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0