6

I have an ASHX file:

Object reference not set to an instance of an object.

On the line:

HttpContext.Current.Session["loggedIn"] = true

Is this how I use sessions properly?

0

2 Answers 2

16

I would guess that Session is the culprit here; with reference here, you might want to try adding : IRequiresSessionState to your handler (the code-behind for the ashx). So you should have something like:

public class Handler1 : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
        context.Session["loggedIn"] = true;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Note also that it is easier to talk to the context passed in, but HttpContext.Current should work too.

Sign up to request clarification or add additional context in comments.

Comments

4

ASHX handlers don't have session information by default.

See this page http://www.hanselman.com/blog/GettingSessionStateInHttpHandlersASHXFiles.aspx

IRequiresSessionState 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.