tags:

views:

45

answers:

2

Hello,

I have an ASP.NET web form application. In the web.config associated with this application, I handle my custom error like so:

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

When a user gets to the error.aspx page, I want to determine what error caused this page to get reached. Does anyone know how I can do this?

Thank you!

+6  A: 

You get the exception object with the GetLastError method:

Exception ex = Server.GetLastError();

(Copied straight out of the code of our error page, which has logged several million errors so far... :)

Guffa
+1 But, ouch. Good luck with those logs ;)
Daniel Dyson
@Guffa: I shall hope that you are referring to a site that has seen extensive use over a long period of time ;)
Fredrik Mörk
@Fredrik Mörk: Yes, we have about 300 request per second, so it's not so many of them that ends up in the log. :) Buggy spiders and buggy IE prefetch causes a lot of the errors. :P
Guffa
+4  A: 

You can do it using Server.GetLastError Method

Exception LastError;
String ErrMessage;

LastError = Server.GetLastError();

if (LastError != null)
   ErrMessage = LastError.Message;
else
   ErrMessage = "No Errors";

Response.Write("Last Error = " + ErrMessage);
Claudio Redi