lance
lance

Reputation: 16342

Determine if .NET sent me to custom error page?

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?

Upvotes: 0

Views: 279

Answers (3)

lance
lance

Reputation: 16342

Code:

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

I'm hoping for something better?

Upvotes: 0

Paulo Santos
Paulo Santos

Reputation: 11567

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.

Upvotes: 2

Dustin Laine
Dustin Laine

Reputation: 38503

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.

Upvotes: 0

Related Questions