locked
No session data available in HttpModule RRS feed

  • Question

  • User-1896412779 posted

    Hello.

     I have a custom HttpModule that I wrote for global exception handling.  I am having a problem where I don't have access to the session object when a 404 error occurs.  This only happens when a 404 error occurs; when my application throws an exception I will have access to the session.  I am wondering why this is and if there is anyway I could get access ot the session?  I would at least be a little happy if I could get the sessionID.  Is there any way that I could get the session ID?  The sessionID never seems to show up in the cookies collection no matter what (not just 404 errors) and I cannot find a way to access the raw request data.  Is there any way I view the raw request text?  The request object has a SaveAs() method (ie. Request.SaveAs() ) but I do not want to save it to disk I want to assign it to a variable in memory and read it, is there no way to do that without saving it to disk first?  I don't see why I cannot simply access the http request data that came in and find the session id and start the session at this point.  Your help would be greatly appreciated. Thanks!

     Here is my HttpModule code:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Web;

    using System.IO;

    namespace CompanyName.ProjectName.Application

    {

    public class ErrorModule : IHttpModule

    {

    public void Init(HttpApplication context)

    {

    context.Error +=
    new System.EventHandler(OnError);

    }

    public void OnError(object sender, EventArgs args)

    {

    HttpApplication application = (HttpApplication)sender;

    HttpContext context = application.Context;

    //Cookies collection count is zero.
    HttpCookieCollection cookieCollection = context.Request.Cookies;

    //Session will be null.
    System.Web.SessionState.HttpSessionState session = context.Session;

    //An exception will be thrown here because the session object is null on 404 errors.
    string sessionID = session.SessionID;context.Response.Write("error has occured");

    context.Server.ClearError();

    }

    public void Dispose()

    {

    }

    }

    }

    Thanks so much!

     

    Sunday, July 1, 2007 2:06 PM

All replies

  • User-1001075126 posted
    I don't know the solution to your problem, byt, if you do not find a way to solve it, try using the void Application_Error(object sender, EventArgs e)

    {

    // Code that runs when an unhandled error occurs

    }

    event on the Global.asax to see if you have access to the session object there [:)]

    Sunday, July 1, 2007 6:05 PM
  • User-1896412779 posted

    I actually tried that already but thanks for the suggestion.  Anybody else? :(

    Sunday, July 1, 2007 6:10 PM
  • User-900677162 posted

    HI,  monarlte:

    When an unhandled exception occurs in ASP.NET the current session will be ended and a new session is started - that makes the former session items unavailable. In your specific case, you can persisting session in Application_Error,:just call

    Server.ClearError();

    If i misunderstand you about your question, please feel free to correct me and i will try to help you with more information.

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

    Monday, July 2, 2007 11:04 PM
  • User-1896412779 posted

    Thanks Rex, but that doesn't help.  I called Server.ClearError() on my current HttpContext object before trying to access session, but my session is still null.  This only happens during the 404 error.  However, on the page I redirect to after the error I still have access to the session.  The objects stored in session are still in session.  So, that session was not destroyed, it is still there.  I'd sure appreciate any further insight.

    Thank you.

    Tuesday, July 3, 2007 10:28 AM
  • User-900677162 posted

    HI, monarlte:

    I will try my best to help you.

    So in Application _Error event or in HttpModule do you want to access the session now.

    The approach about Server.ClearError() should be used in Application _Error event.

    You said 'on the page I redirect to after the error I still have access to the session', i suspect it should be a new session after the error occurs. So you can have check to the sessionid before and after the error occurs.

    If i misunderstand you about your question, please feel free to correct me and i will try to help you with more information.

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

    Tuesday, July 3, 2007 10:04 PM
  • User-1896412779 posted

    Nope, that doesn't do the job.  I think I am going to give up and use a hacked solution.  :(

    Wednesday, July 4, 2007 12:43 PM
  • User-506642330 posted

     Hey can you post that solution... i am stuck with that too.

    Thursday, February 12, 2009 12:20 PM
  • User-506642330 posted

      Hey i have custom exception handler module, in which error message is store in session and then its displayed on a custom error page but issues is that when reference is back from the httpmodule session is regenerated.

     As you say so to use Server.ClearError(); well it doesnt redirect me to error page, as i have configuration in web.config <customErrors mode="On" defaultRedirect="~/pages/error/genericerror.aspx">

    Thursday, February 12, 2009 12:30 PM