Question

web.config:

<customErrors mode="On" defaultRedirect="~/Foo.aspx" /> 

When Foo.aspx.cs is running, how can I know that an uncaught exception is what sent me to Foo.aspx?

Was it helpful?

Solution

Check the Server.GetLastError() and also check the Response.StatusCode to determine why the page has been called.

If you set the customErrors element on the web.config the defautRedirect page will only be called when an unknown state occus, that is, if you specify custom pages for status codes 404 and 403, for instance, your foo.aspx page will only be called when a different status appears.

OTHER TIPS

void Application_Error(object sender, EventArgs e)
{
    HttpContext ctx = HttpContext.Current;
    Exception exception = ctx.Server.GetLastError();
    ctx.Server.ClearError();
    ctx.Server.Transfer("Foo.aspx?ERROR" + exception.Message);

}

This method will fire before you go to Foo.aspx, so you can catch that you are coming from an error and not a redirect. You can then append a QueryString variable to the url so that Foo.aspx can work with that data.

Not sure what your end goal it, but if you are trying to customize error message appears based on the exception you can handle it this way.

Code:

if (!string.IsNullOrEmpty(Request["aspxerrorpath"])) {
    ....
}

I'm hoping for something better?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top