By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,012 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,012 IT Pros & Developers. It's quick & easy.

Application_Error, Session, and custom error page

P: n/a
I am trying to write a global custom error page. I thought I would jot down
some of what I've learned so far...

At first I just used the default customErrors section with a defaultRedirect
tag, as such:

<customErrors defaultRedirect="error.aspx" mode="On" />

First thing I found out about this method, is that I didn't have access to
the exception object in my error.aspx page. So this page was going to be
very uninformitive. It appears that the Server.GetLastError cannot be
called outside of the Application_Error event in the global.asax file.

I found this asp.netPRO example that I am now using as a starting point:
http://www.aspnetpro.com/features/20...200305dk_f.asp

In the article the author saves the Exception in a "global" module variable
(how strange) so that he could use it later in his custom error page. This
worked Ok, but this doesn't appear to be thread-safe. So I quickly decided
I should be using a Session variable instead. The author hints that there
are other methods of doing this, but wisely avoids the whole issue. :)

Lots of examples on Google of people trying to stuff their Exception into a
Session("LastError") in the Application_Error event. Just as many people
wondering why it isn't working. ;(

Turns out you can write to the Session object in the Application_Error
event, but it won't save it unless you also do a Server.ClearError. Ouch!
Take a look http://tinyurl.com/r3xk for a quick explanation.

So now my Application_Error event is looking like this:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

'Grab a reference to the last error. Use InnerException because
'ASP.NET wraps errors here in an HttpUnhandledException.

Session("LastError") = Server.GetLastError.InnerException

Server.ClearError() ' must clear error else session state will not
be preserved!!!

' since manually clearing error, must manually redirect...
'Server.Transfer("~/error.aspx")
Response.Redirect("~/error.aspx")

End Sub

I have also taken out the defaultRedirect="error.aspx" tag in the web.config
since this will make it pointless(?)

Notice, I abandoned the server.transfer call in favor on the
response.redirect. Every single time I try and use server.transfer the page
will ignore my style sheet(!) Anybody?

Since my app is also using forms authentication, I went back an made sure my
error page was accesible whether logged in or not...

<location path="Error.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>

I have seen people using this syntax in their web config: <location
path="~/Error.aspx">, but this doesn't appear to work (or even be
necessary).

Anyways, I hope this helps somebody out there.

Greg


Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Me
Because the error page is in a different directory from
the page where the error occurred. When Server.Transfer
is called, the processing switches to the new page (error
page in this case), but the URL in the browser is still
the initial page where the error occured. So the error
page is effectively being displayed in a different
directory than it's actual location.... causing it not to
be able to find the CSS.

-----Original Message-----

Notice, I abandoned the server.transfer call in favor on
the
response.redirect. Every single time I try and use
server.transfer the page
will ignore my style sheet(!) Anybody?

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.