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?
Asked
Active
Viewed 4.5k times
46
-
"HttpContext.Current.Session" i have try this butt always get exception. – Kashif Hanif Jul 12 '12 at 07:50
-
4What exception you are getting? – mrd Jul 12 '12 at 09:38
-
@mrd it is showing NullReference Exception in .ashx file. I followed this post but still getting exception. – Shyam Dixit Jan 03 '14 at 07:59
-
it works fine for me...can you also try using this....System.Web.SessionState.IReadOnlySessionState or System.Web.SessionState.IRequiresSessionState....?? – Kashif Hanif Jan 03 '14 at 08:44
-
@KashifR I already included this. Actually it is showing null in the variable.I dont know why. In .aspx file value stored in variable but while getting it in .ashx file it is not showing. – Shyam Dixit Jan 03 '14 at 09:03
-
can you post your code here....so that i can check..please also make header like this public class FileUpload : IHttpHandler,System.Web.SessionState.IRequiresSessionState Then get in session like this spaceused = context.Session["SpaceUsed"] + ""; – Kashif Hanif Jan 03 '14 at 19:56
3 Answers
79
In the ashx.cs file, also "implement" the interface System.Web.SessionState.IReadOnlySessionState
or System.Web.SessionState.IRequiresSessionState
.
You don't have to implement any method, just the presence of this makes the Session available (in readonly or read/write mode), through context.Session
.
The header would look like:
public class MyHandler: IHttpHandler, System.Web.SessionState.IReadOnlySessionState

Hans Kesting
- 38,117
- 9
- 79
- 111
-
4I love you. For some reason this issue was only manifesting for me in Visual Studio 2013 (VS2013). Could be because of the .NET version or IIS Express Version it uses (IIS 7?) Thanks for the help! – Suamere Mar 16 '14 at 21:35
-
While this answer is helpful, the accepted answer also includes this in the first line of the example code: public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState – Scott R. Frost Dec 17 '18 at 14:30
58
In aspx file:
Session.Add("filename", "Test.txt");
After you have set session value in aspx file. Use following to get the value in ashx file.
In ashx file:
public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string Name = "";
if (context.Session["filename"] != null)
Name = context.Session["filename"].ToString();
}
}

mrd
- 2,095
- 6
- 23
- 48
-
How to set a session variable? context.Session["filename"] = "somevalue"; is it right? – efirat Sep 15 '12 at 11:42
-
1If you are setting session variable in aspx page then just use Session("filename") = "somevalue". And if you are setting in handler as above then use context.Session["filename"] = "somevalue". – mrd Sep 17 '12 at 11:50
-
16They key part of this answer is that the handler must be modified to inherit from IRequiresSessionState in order to have access to the Session. – James in Indy Dec 24 '13 at 12:58