Vitor Reis
Vitor Reis

Reputation: 989

Add a web.config key for always redirect when get an unhandled exceptions

I once saw that was possible to do something like adding a key in the web.config file to redirect to a default error page everytime a unhandled exception is found.

Is it possible? how?

Upvotes: 3

Views: 1492

Answers (3)

Jon Hanna
Jon Hanna

Reputation: 113272

<customErrors defaultRedirect="~/serverErrorPage.aspx" mode="On" redirectMode="ResponseRewrite"/>

It's not suitable for real use prior to .NET3.5 service pack 1, as until then the redirectMode attribute wasn't there and it would always act with the default value "ResponseRedirect" which would redirect to the error page instead of showing it directly; so instead of giving an error response it would "successfully" redirect to another page, and then that would return the error!

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38503

Yes the customErrors section of the web.config.

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

This will redirect your users to what defaultRedirect (URL) when they encounter an error.

You can also specify where they go based on the HTTP response code

<customErrors defaultRedirect="~/GenericError.aspx" mode="On">
    <error statusCode="500" redirect="~/Error.aspx"/>
    <error statusCode="404" redirect="~/NotFound.aspx"/>
</customErrors>

Here is the documentation.

Upvotes: 6

Jason Berkan
Jason Berkan

Reputation: 8884

Add a CustomErrors section to your web.config.

<customErrors defaultRedirect="ErrorPage.aspx" mode="RemoteOnly" />

Upvotes: 5

Related Questions