Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 20-Apr-11 0:46am
zan0701591
Comments
shakil0304003 at 20-Apr-11 6:42am
   
Use google 1st!!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Implement the System.Web.SessionState.IRequiresSessionState[^] interface to your handler.
 
Now you can access the Session using context.Session from ProcessRequest method
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Venkatesh Mookkan at 20-Apr-11 6:00am
   
You are faster than me. Good Answer!
Susovan biswas at 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 at 20-Apr-11 6:06am
   
Thnaks :)
zan0701 at 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 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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;
        }
    }
 
}
  Permalink  
v2
Comments
Prasanta_Prince at 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)

  Print Answers RSS
0 Maciej Los 230
1 OriginalGriff 202
2 Richard MacCutchan 185
3 Tomas Takac 146
4 CPallini 125
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 20 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100