The 2 most common HTTP errors that occur are the 404 and 403. 404 is the File Not Found error and it is generated when the user clicks on a broken link or enters a file or folder name that does not exist in the server. And the 403 Access Denied error is generated when the user tries to access a page he/she has no permissions to. These errors can be handled by IIS or the ASP.NET website, here I will show the latter.

To start handling these errors, 3 pages need to be created in the root folder of the project, one for each error and the last one will be displayed when an unhandled error occurs. Its always better to keep the code in these pages to a minimum and dont access databases, the file system, or any external pages. And the last thing to make sure of is anonymous users have access to these pages.

To use them, they should be included in the web.config file, in the element. The mode attribute is required, and it sets whether the custom errors are enabled or not. Its third option is RemoteOnly and this will display the custom page to remote users only, i.e. if the website is accessed from the local host the customErrors element will not work. The defaultRedirect attribute sets the page that will displayed if the error occurred is not a 404 or a 403 error.

<customErrors mode="RemoteOnly" defaultRedirect="errorpage.aspx">
    <error statusCode="403" redirect="noaccess.aspx" ></error>
    <error statusCode="404" redirect="filenotfound.aspx"></error>
</customErrors>