Saturday, March 10, 2012

Getting Sessions in HttpHandlers (ASHX files)

To read or assign session values in *.ashx files just add IRequiresSessionState or IReadOnlySessionState
as follows.....


<% @ webhandler language="C#" class="ShowImage" %>

using System;
using System.Web;
using System.Web.SessionState;



public class DownloadHandler : IHttpHandlerIRequiresSessionState, IReadOnlySessionState
{  
   public bool IsReusable { get { return true; } }

   public void ProcessRequest(HttpContext context)
   {

       // assign value to Session 
       context.Session["userID"] = "1001";
              
      // read value from Session 
             string username = context.Session["username"] = "vara prasad reddy";
   }
}


                            OR




<% @ webhandler language="C#" class="ShowImage" %>

using System;
using System.Web;
using System.Web.SessionState;


public class DownloadHandler : IHttpHandler, IRequiresSessionState
{  

   public bool IsReusable { get { return true; } }

   public void ProcessRequest(HttpContext context)
   {

       // assign value to Session 
       context.Session["userID"] = "1001";
              
      // read value from Session 
             string username = context.Session["username"] = "vara prasad reddy";
   }
}

No comments:

Post a Comment