Custom error mode in ASP.NET web.config
As a developer, I would love to see the error page that provides details of the error, status code and also the stack information. This information is useful to debug the application but soon becomes an ugly yellow page when I switched my role from developer to user.
Who wants to see an ugly yellow page? Just a developer (sigh!)
Here comes the custom error page to the rescue, from yellow page,to indicate an error. We just have to bring some configuration changes in the ><customErrors> section of our web.config file.
How we add custom error configuration?
Custom 404 and 500 errors are typically used to redirect the user to the error page.
Let’s see an example,
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors>
We can see that the mode selected is RemoteOnly. we have, in all 3 modes:
1) Off Mode: It signifies that custom errors are disabled. In this mode, the same ugly error page is shown which we, as a developer, consider helpful for debugging purpose.
2) On Mode: It signifies custom errors are enabled, and as discussed above, user is redirected to a custom error page. In the above example, user may be redirected to NoAcess.htm or FileNotFound.htm on specific errors.
3) RemoteOnly Mode: Same as ON Mode, custom errors are enabled. The difference lies in the request originating from, like if the request originates from a remote machine page is redirected to custom error page else if the request originates from the local machine the default error page is shown.
Selecting a mode is a mandatory action.The default mode is RemoteOnly.
We have “defaultRedirect” element which is optional and as the name suggests, user is redirected to a default URL.
Two lines of configuration and relaxed eyes of user enhances user’s experience.