Question

I am trying to do an error page which redirect to it whenever an error occurs.

This is my code:

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

which is working fine now how can I get the error message too on my error page

Example: Error - Index Error

Was it helpful?

Solution

You would need to get the last error that occurred (programmatically) and display it in the page. You can do that like this (in Error.aspx):

protected void Page_Load(object sender, EventArgs e)
{
     Exception ex = Server.GetLastError();
     lblError.Text= ex.Message;
     Server.ClearError();
}

Where lblError is a Label control defined in your page just for the purpose of displaying the error messages.

See here for more details.

OTHER TIPS

protected override void OnError(EventArgs e)
{     
  HttpContext ctx = HttpContext.Current;

  Exception exception = ctx.Server.GetLastError ();

  string errorInfo = 
     "<br>Offending URL: " + ctx.Request.Url.ToString () +
     "<br>Source: " + exception.Source + 
     "<br>Message: " + exception.Message +
     "<br>Stack trace: " + exception.StackTrace;

  ctx.Response.Write (errorInfo);
  ctx.Server.ClearError ();

  base.OnError (e);
}

Read more about ASP.NET Custom Error Pages

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top