Tuesday, July 31, 2012

Getting session state in httphandlers - ashx files

To do session state handling in Generic handlers or ashx files, there are two possibilities Readonly session state: we need to implement IReadOnlySessionsState interface, usefollowing demo code.
using System;
using System;
using System.Web;
using System.Web.SessionState;

public class Handler : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {
    if (HttpContext.Current.Session["SessionsName"] != null)
     {
      context.Response.Write(HttpContext.Current.Session["SessionsName"]);
     }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
Read and Write Access to state: we need to implement IRequiresSessionState interface, usefollowing demo code.
using System;
using System.Web;
using System.Web.SessionState;

public class Handler : IHttpHandler, IRequiresSessionState
{

 public void ProcessRequest(HttpContext context)
  {
   if (HttpContext.Current.Session["SessionsName"] != null)
    {
      HttpContext.Current.Session["SessionsName"] = "Demo Session";
     }
   }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
Submit this story to DotNetKicks

0 comments: