Identify the error that caused the redirect in ASP.NET

I have an ASP.NET web form application. In the web.config file associated with this application, I handle my own error as follows:

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

When the user gets to the error.aspx page, I want to determine what error led to reaching this page. Does anyone know how I can do this?

Thank!

+3
source share
2 answers

You can do this using the 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);
+4
source

You get the exception object using the method GetLastError:

Exception ex = Server.GetLastError();

(Copied directly from the code of our error page, on which several million errors have been registered so far ... :)

+6

Source: https://habr.com/ru/post/1763604/


All Articles