Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to access some value(which is already set in.aspx file) in .ashx file. I tried to get that value using querystring, session etc but each time it failed. Can anyone suggest me how can we access session value in .ashx file?


Thanks
Posted
Comments
shakil0304003 20-Apr-11 6:42am    
Use google 1st!!!

Implement the System.Web.SessionState.IRequiresSessionState[^] interface to your handler.

Now you can access the Session using context.Session from ProcessRequest method
 
Share this answer
 
yes you can access the session in your ashx file. For that you need to implement IRequiresSessionState interface in your handler class.
Then you can access session as context.Session. Here context is the object of class HttpContext, which is passed as a parameter in ProcessRequest method.
 
Share this answer
 
Comments
Venkatesh Mookkan 20-Apr-11 6:00am    
You are faster than me. Good Answer!
Susovan biswas 26-Jul-13 5:25am    
i have implemented IRequiresSessionState interface in the handler. Still the error "object referance not set to an instant of an object " persists
Brij 20-Apr-11 6:06am    
Thnaks :)
zan0701 20-Apr-11 6:17am    
I already tried this way but it did not work for me. :(
in temp.aspx file i assigned value to session:
Context.Session["temp"] = _idalbum;
and in .ashx file i tried this:
string allfolder = context.Session["temp"].ToString();
which caused error -object referance not set to an instant of an object because session is null.
Brij 20-Apr-11 6:30am    
did you implemented IRequiresSessionState interface in the handler. Without this, you would not be able to access..
If after doing doing this, you are not getting the session data, means there is saome other problem. I have used in this way so many times. try accessing session in any page and check whether it is accessible?
Example:
<![CDATA[<%@ WebHandler Language="C#" Class="AddGiftToRegistryImageHandler" %>

using System;
using System.Web;

public class AddGiftToRegistryImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        
            string Name = "";

            if (context.Session["Name"] != null)
                Name = context.Session["Name"];
            

        context.Response.ContentType = "text/plain";
        context.Response.Write(Name);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
 
Share this answer
 
v2
Comments
Prasanta_Prince 20-Apr-11 8:23am    
Good Solution.

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