Get Value Of Session variables from Generic Handlers
if we are trying to access the value of session variables from Generic Handlers(.ashx files) like we acces from codebehind (.cs file) as the code below
public class MyHandler: IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string sessionValue = Convert.ToString(HttpContext.Current.Session["LoginUser"]);
}
}
we got NullReferenceException or Object reference not set to an instance of an object.
Solution
To overcome this exception and to access session variables from the HttpHandler, we should inherit the interface IReadOnlySessionState or IRequiresSessionState
(For this include the name space System.Web.SessionState first).
IReadOnlySessionState provides read-only access to session variables. And
IRequiresSessionState provides read/write access to session variables.
The working code is like this
using System.Web.SessionState;
public class MyHandler: IHttpHandler,IRequiresSessionState {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string sessionValue = Convert.ToString(HttpContext.Current.Session["LoginUser"]);
}
}
it is working only in IE not in mozilla, chrome etc….
this is not working
Hi what error you are getting? Make sure you are using the required namespace