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

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe useful your great article (from 2004) in MSDN msdn.microsoft.com/en-us/library/aa479319.aspx and Build a Really Useful ASP.NET Exception Engine _ nullskull.com/articles/20030816.asp codeproject.com/Articles/155810/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.