views:

51

answers:

3

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?

+5  A: 

Add a CustomErrors section to your web.config.

<customErrors defaultRedirect="ErrorPage.aspx" mode="RemoteOnly" />
Jason Berkan
Note the remoteonly will behave such that if you access the site from the local lan/intranet then it will not perform the redirect. The reason for this is so local developers and see the actual exception occuring while those accessing it from the public will see the ErrorPage.aspx
AaronLS
+5  A: 

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.

Dustin Laine
+1 for in depth with doc link
Michael Wheeler
A: 
<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!

Jon Hanna