locked
HttpContext.Current.Session in Custom HTTP Handler RRS feed

  • Question

  • User-594738727 posted

    I am using HttpContext.Current.Session to retrieve session variables within a HTTP Handler in my application.  This is working fine, however I also want to set a session variable, how do I do this as HttpContext.Current.Session appears to only be able to "Get".

    Here is the peice of code I am talking about:

    if (HttpContext.Current.Session["pdfDocument"] != null)
                {
                    test = (Boolean)HttpContext.Current.Session["pdfDocument"];
    
                    if (test == true)
                    {
                        //delete the variable
    
                        //next line not working!!
                        HttpContext.Current.Session["pdfDocument"] = false;
                       
                        String filepath = "/Documents/";
                        String document = System.Web.HttpContext.Current.Session["docName"].ToString();
    
                        objResponse.TransmitFile(filepath + document);
                        objResponse.End();
                        
                    }
    
                    
    
                }
     As you can see I am simply attempting to set Session["pdfDocument"] to false in order to prevent the statements from running again, however it seems to be completely ignoring this statement.  It is being run as it hits a breakpoint.
    Monday, March 30, 2009 10:05 AM

All replies

  • User854688209 posted

     The code which you have posted is working perfectly fine .

    I have commented the below line and it works fine.

    objResponse.TransmitFile(filepath + document);
    objResponse.End();

    I think even you put the above line in ur code, it should work.

    Where are you setting the value in HttpContext.Current.Session["pdfDocument"] . R u missing to set the value inHttpContext.Current.Session["pdfDocument"].

    Monday, March 30, 2009 11:59 AM
  • User-594738727 posted

     Hi, many thanks for your reply! :-)

     I'm very sorry but I don't understand your answer :-S which line did you comment out?

    Monday, March 30, 2009 12:51 PM
  • User1622957740 posted

    Are you implementating IRequiresSessionState on your handler? It's required in order have full access to session state. I believe read access works without but updates require this interface.

    http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate.aspx

    +++ Rick ---

    Tuesday, March 31, 2009 4:17 AM
  • User-594738727 posted
    Ahh nailed it on the head in one! :-) I was using IReadOnlySessionState :-S  I didn't even think to look up to the top of my code....assumption is a wonderful thing.
    Tuesday, March 31, 2009 4:31 AM