Handle Error Modes in ASP.NET redirect to proper page - Web.config

Most of the websites and web applications must have an error handler. Some programmers might question what are the error handler should have on one website. Below is the basic error handler that you need to set in the website :
  1. Page Not Found - 404
  2. Internal Server Error - 500
  3. Access Denied - 403
The above error page must-have in one website. This is because when the application catch error, the end-users will see asp.net designed page without detailed error information that might not user friendly for the end-users. To handle these errors, all you need to do are two things which are Create the above error page and set in the web.config file in asp.net.

Below is an example of a setting in the web.config file.


<customErrors defaultRedirect="~/500.html?" mode="RemoteOnly">
<error statusCode="404" redirect="~/404.html" />
<error statusCode="500" redirect="~/500.html" />
<error statusCode="403" redirect="~/AccessDenied.html" />

As you can notice, I set the default error to defaultRedirect="~/500.html?". The symbol "?" is to avoid web application passed unwanted query string when the web application catches the error.

Modes Available List

Error ModeDescription
RemoteOnlyError pages are shown for all end-user except localhost users. For example, if your website is accessible by domain abc.com, so everyone who accesses the website using abc.com will see the serve the error page. If you access the website using localhost / - you will not see the error page
OffAll users will see the default error page by asp.net with details error information such as line number error and some of your coding highlights by the compiler where the error caught.
OnNo matter local or remote user will see the error page created by you (404.html / 500.html or AccessDenied.html)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example