Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i get session value in Handler.ashx. I am getting Session as null

<%@ WebHandler Language="C#" Class="Upload" %>

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


public class Upload : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    events evtObj = new events();
    public void ProcessRequest(HttpContext context)
    {        
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        try
        {           
            HttpPostedFile postedFile = context.Request.Files["Filedata"];
            string savepath = context.Server.MapPath("eventfiles");
            string filename = Path.GetRandomFileName() + postedFile.FileName;
            postedFile.SaveAs(savepath + @"\" + filename);
            context.Response.Write("eventfiles" + "/" + filename);
            string path = savepath + @"\" + filename;
            evtObj.fileName = filename;            
            evtObj.eventId =  int.Parse(context.Session["_eventId"].ToString()); // SESSION IS NULL.
            evtObj.FilUpload(evtObj);
        }
        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
Posted
Updated 7-Jul-14 19:01pm
v2

When you want to get access to your Session state from an ASHX or HttpHandler, you have to implement IReadOnlySessionState:
C#
public class Upload : IHttpHandler, IReadOnlySessionState
{
   public bool IsReusable { get { return true; } } 
   
  //Add you code from here
  //...
}
 
Share this answer
 
First put sessionvalue in page
Session["_eventId"]=12;

They key part of this answer is that the handler must be modified to inherit from IRequiresSessionState in order to have access to the Session
public class Upload : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
...

 if(  context.Session["_eventId"] !=null))
  evtObj.eventId =  int.Parse(context.Session["_eventId"].ToString())

....
 
Share this answer
 
Comments
[no name] 8-Jul-14 0:59am    
still i am not able to get session value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900