Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

c# - 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>
question from:https://stackoverflow.com/questions/14181408/httpcontext-current-session-is-null-in-ashx-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...