Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Current.Session is null in Ashx file

I saw some questions (Here and Here) but they do not answer my question. I am trying to call Ajax using "ajax.ashx" file, and in function to access Session. For some reason, the value of the Session object itself is null.

Use example:

Session = HttpContext.Current.Session // This is null 

Or:

public virtual void ProcessRequest(HttpContext context) {     System.Web.SessionState.HttpSessionState Session = context.Session;      // This is null } 

In the Web.config:

<sessionState timeout="1800"></sessionState> 
like image 971
Mosh Feu Avatar asked Jan 06 '13 10:01

Mosh Feu


People also ask

Why HttpContext current session is null?

Session will never be null if you have any variable in the Session. HttpContext. Current. Session represents a collection of all the session values that you are storing in the session.

What is ASHX file in C#?

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 install an ASHX file?

For adding ashx file follow below steps. Right click on Project. Select Add from the menu. In Add New Item dialog window select Generic Handler and click on Add.


1 Answers

You must add on your handler the IRequiresSessionState on the declaration of it as:

public class YourHandleName : IHttpHandler, IRequiresSessionState  { ... 

by default the handlers are not connected with the session to keep them minimum, by adding the IRequiresSessionState you attach them with the session.

like image 101
Aristos Avatar answered Sep 25 '22 02:09

Aristos