Hi,
Please give me an example of page level error handling
a)Using 'ErrorPage' attribute in '@page' directive and
b)Using 'protected override void OnError(EventArgs e)' method.
I tired an example which is mentioned below,
Step 1: In web.config,
<system.web>
<customerrors mode="On">
</customerrors>
</system.web>
Step 2: In my aspx page,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ErrorHandling.aspx.cs" Inherits="TutorialSite.ErrorHandling" ErrorPage="~/ErrorPage.aspx"%>
Step 3: In my aspx.cs page, I tired to navigate to the page 'Page200.aspx', which is not existing.
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("Page200.aspx");
}
protected override void OnError(EventArgs e)
{
// At this point we have information about the error
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo =
"
Offending URL: " + ctx.Request.Url.ToString() +
"
Source: " + exception.Source +
"
Message: " + exception.Message +
"
Stack trace: " + exception.StackTrace;
ctx.Response.Write(errorInfo);
// --------------------------------------------------
// To let the page finish running we clear the error
// --------------------------------------------------
ctx.Server.ClearError();
base.OnError(e);
}
But OnError is not fired after an error and I got the default error page itself.