C# – ASP.NET: Session state not updated when redirecting in explorer

asp.netc

I have an aspx page (page1.aspx) where I set a Session variable and then redirect to another page:

HttpContext.Current.Response.Redirect("page2.aspx");

On page2.aspx I want to read the session variable, but it doesn't exist until I reload the page. I try to read the session variable in page_load.

The weird thing is that it works in firefox, but not in explorer??

(Note that I've simplied this a bit to explain the problem page1.aspx is accessed from my HttpModule that and is rewritepathed (my workaround to get access to Session). So the flow is HttpModule -pathrewrite-> page1.aspx (set session var) -redirect-> page.2.aspx.)

Best Answer

Try switching your Response Redirect to:

Response.Redirect("page2.aspx",false);
HttpContext.Current.ApplicationInstance.CompleteRequest();

The nominal Redirect(url) implicitly calls Redirect("url", true) which throws a ThreadAbortException. The ThreadAbortException raised is a special exception when it is done as such inside ASP.NET. This exception while it can be caught cannot be muted. It will continue to bubble up the ASP.NET call chain to cause the current worker thread to immediately terminate.

It's possible that the abrupt termination could be impacting this.

Related Topic