More on customErrors and session state

In an earlier post I made mention of my observations that the Session variables are cleared when ASP.NET uses its <customErrors defaultRedirect=”YourErrorPage.aspx” mode=“On“ /> section, because noone managed to catch an exception.

Since then I came across this article by Eri Robillard, that was posted on the 27th of January. I must have missed it, and somehow it didn't show up during my Google session on the matter.

Although the article doesn't explain why the session variables are cleared, it does mention the ways to pass control after an exception that occured in combination with the manner of passing (parts of) the exception along.

From the article:

Storage BasketControl-passing methods that work
ApplicationResponse.Redirect(), Server.Transfer(), or customErrors:defaultRedirect
CookiesResponse.Redirect(), Server.Transfer(), or customErrors:defaultRedirect
Context, SessionServer.Transfer()
QueryStringResponse.Redirect() or Server.Transfer()

So, in this respect, you could use the <customErrors> section if you change the code ínside Global.asax to:

HttpUnhandledException ex =
    (HttpUnhandlesException)Server.GetLastError();
Application.Lock();
Application[Session.SessionID + “Exception“] = ex.InnerException;
Application.Unlock();
// Do not call Server.ClearError, so the exception will be caught
// by ASP.NET, which will evaluate <customErrors>

Obviously, the YourErrorPage.aspx would then retrieve the exception from the Application variable like so:

Exception ex = Application[Session.SessionID + “Exception“];

I haven't tried this out yet, nor have I checked if the behavior is different in Whidbey. Let me know if you tried or check it already.

Comments

# Max said:

Another Microsoft's article:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskDisplayingSafeErrorMessages.asptalks about saving Server.GetLastError().Message in a Session variable...Session[&quot;CurrentError&quot;] = &quot;Global: &quot; +         Server.GetLastError().Message;    Server.Transfer(&quot;lasterr.aspx&quot;);their code...Is it possible that the Session variable is not assigned at all?Instead of Server.Trasfer try:Response.Write(Session[&quot;CurrentError&quot;]);Response.End();

vrijdag 9 april 2004 16:35