Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Session is null in ashx file

I am trying to access the Session variable in Asp.Net ashx handler as shown below.

public void ProcessRequest (HttpContext context) {
        context.Session["VariableName"] = Id;
    }

But the context.Session is always Null inside the above method. How do I access Session objects in ashx file?

like image 828
Amitabh Avatar asked Jan 28 '10 17:01

Amitabh


People also ask

What is ASHX file in asp net?

What is an ASHX file? An ASHX file is a webpage that is used by the ASP.NET HTTP Handler to serve user with the pages that are referenced inside this file. The ASP.NET HTTP Handler processes the incoming request, references the pages from the . ashx file, and sends back the compiled page back to the user's browser.

How do I make an ASHX file?

Go to the Website menu and click on the first menu item there, "Add New Item". This will present the Add New Item dialog box. Then Select the "Generic Handler" item, and you will get a new file with some code in it called Handler. ashx.


2 Answers

You have to "implement" either IRequiresSessionState or IReadOnlySessionState, with former providing full access to session, and the latter providing read-only access.

I'm quoting "implement" here because these two are so-called "marker interfaces", which means they have no members.

like image 96
Anton Gogolev Avatar answered Sep 19 '22 11:09

Anton Gogolev


In VB, implement the interfaces mentioned by Anton (IRequiresSessionState or IReadOnlySessionState) like this:

Public Class MyAshxFile      Implements System.Web.IHttpHandler     Implements System.Web.SessionState.IRequiresSessionState ''need this for session variables     Implements System.Web.SessionState.IReadOnlySessionState ''need this for session variables 
like image 27
Taylor Brown Avatar answered Sep 17 '22 11:09

Taylor Brown