Instead of Response.Redirect
, which sends a response to the client asking it to request a different page, you should call Server.Transfer
, which runs a different page immediately and sends that page directly to the client.
You can then put the exception in HttpContext.Items
and read it from HttpContext.Items
in your error page.
For example:
catch (Exception ex) {
HttpContext.Current.Items.Add("Exception", ex);
Server.Transfer("Error.aspx");
}
In Error.aspx
, you can then get the exception like this:
<%
Exception error;
if (!HttpContext.Current.Items.Contains("Exception"))
Response.Redirect("/"); //There was no error; the user typed Error.aspx into the browser
error = (Exception)HttpContext.Current.Items["Exception"];
%>