1

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

Mark Fenech
  • 1,308
  • 6
  • 26
  • 36
  • I think this is a similar kind of a question [Server.GetLastError()](http://stackoverflow.com/questions/343014/asp-net-custom-error-page-server-getlasterror-is-null) – huMpty duMpty Jan 11 '13 at 16:17

2 Answers2

1

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.

Icarus
  • 60,193
  • 14
  • 91
  • 110
  • Maybe useful your great article (from 2004) in ***MSDN*** _https://msdn.microsoft.com/en-us/library/aa479319.aspx_ and **Build a Really Useful ASP.NET Exception Engine** _ http://www.nullskull.com/articles/20030816.asp http://www.codeproject.com/Articles/155810/Back-to-the-Basics-Exception-Management-Design-Gui_ – Kiquenet Sep 08 '15 at 10:06
1
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

huMpty duMpty
  • 13,481
  • 12
  • 52
  • 88