How to get Error info in Custom Error page

R

Richard

I have been struggling with this for days. I feel I must be missing something simple, and I will be eternally grateful for any help. I'm using VS.NET 2003 on XP Pro Sp2.

I searched and found several examples of Custom Error pages for ASP.NET and nothing seemed to work. I zeroed in on "Server.GetLastError" and found it was always "Nothing".

This is in my web.config:
<customErrors mode="On" defaultRedirect="error/errors.aspx" />

In errors.aspx, I copied the code below exactly from MSDN "GetLastError Method" sample.
When I step through the code from an intentional 404 error, LastError is always "nothing". Any idea why? How do I get some useful error info so I can display a custom message for 404 errors, and something else for other errors.

'--- errors.aspx
Dim LastError As Exception
Dim ErrMessage As String

LastError = Server.GetLastError()

If Not LastError Is Nothing Then
ErrMessage = LastError.Message
Else
ErrMessage = "No Errors"
End If

Response.Write("Last Error = " & ErrMessage)
 
J

James Steele

Hi Richard,

Its to late to catch the error at that point.

Instead you should store the Exception inside the Application state bag
from within your global.asax codebehind

Something like...

protected void Application_Error( object src, EventArgs e ) {
Exception objMyError = Server.GetLastError();

HttpContext.Current.Application.Add("lastError",objMyError);
Server.ClearError();
}

Then in the page_load of errors.aspx you can retrieve the message from
the application object and have ALL the information you need

Inside the page_load you can access the application object you
stored...

HttpContext.Current.Application.Get("lastError")

Then you can access all the properties of GetBaseException class!!!

Have fun. Let me know how you make out!
 
B

Brock Allen

Its to late to catch the error at that point.

James has the right idea, but the implementation suggestion is misguided.
protected void Application_Error( object src, EventArgs e ) {
Exception objMyError = Server.GetLastError();
HttpContext.Current.Application.Add("lastError",objMyError);
Server.ClearError();
}

Then in the page_load of errors.aspx you can retrieve the message from
the application object and have ALL the information you need


This is not safe, since ASP.NET is multi-threaded. You could have two requests
fail at the same time and this code only stores one of those errors. Then
errors.aspx might have someone else's error.

Unrelated to this issue, I'd suggest your error page not use the exception
information at all and instead just show a nice friendly message to the user.
As for the actual error (Exception object) do take James' advice and handle
the Application_Error in global.asax, get the error via Context.Error and
log the Exception by writing it to a log file, or the system EventLog or
email it to an admin or save it by some other means. You should keep that
information so that an admin can look at it. You specifically don't want
to show any of that information to an end user as they could be malicious
and use it against you and the application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top