Use and access Session variables in Generic Handler in ASP.Net
By default Session is disabled inside the Generic handler and hence in order to use and access Session variables we need to inherit the IRequiresSessionState interface.
C# Page:
protected void Submit(object sender, EventArgs e)
{
Session["Name"] = txtName.Text;
Response.Redirect("~/HandlerCS.ashx");
}
Generic Handler:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Web.SessionState;
public class Handler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
string name = context.Session["Name"].ToString();
context.Response.ContentType = "text/plain";
context.Response.Write(name);
}
public bool IsReusable {
get {
return false;
}
}
}