views:

42

answers:

2

I understand that I can redirect a user on an exception by setting the customErrors element in the Web.config

<customErrors mode="On" defaultRedirect="/WebTest/ErrorPages/AppError.html">
</customErrors>

I could also do a Response.Redirect in the Application_Error event in the Global.asax file.

What are the differences between these 2 approaches and which one is preferred?

+5  A: 

If you don't want to do anything with the error (such as logging), you'd better simply use the configuration file. It's simpler and easier to reconfigure. Moreover, using the configuration file has the advantage of supporting RemoteOnly type, where you can easily see the exception on the server for diagnostics purposes but others won't be able to see it.

In general, there's no point in writing code for something that's perfectly configurable and is built in the system. Code is more prone to errors than configuration options.

Mehrdad Afshari
I do log on Application_Error. Are you saying that with the config setting, control never goes through Application_Error?
No, I'm not saying that. I'm saying that if you don't have any specific work to do with the exception, like logging or choosing where to redirect based on some property of the exception, you'd better off stick to the config file. It's possible to use both. Config for the actual redirection and `Application_Error` for additional processing.
Mehrdad Afshari
A: 

I am not sure of anything official, but I find using the config file easier for local testing versus production behaviour.

Jonathan Bates