Reputation: 1358
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
Upvotes: 1
Views: 229
Reputation: 14460
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
Upvotes: 1
Reputation: 63956
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.
Upvotes: 1