Custom Errors in ASP.Net

Introduction 

When a run-time or design-time error occurs in an application, ASP.NET shows a default Yellowish-Red error page that gives a brief description of the error along with some status code. If this page is viewed by the developer, then he can analyse the actual reason by its status code, but if a user or anyone other than the developer is viewing this error page, they won't get the actual idea of why this error is coming. So to provide a friendly error message page we need to set custom error in web.config.
 
Some of the basic common errors with status codes are:

Error Code Description
400 Bad Request The request cannot be fulfilled due to bad syntax
403 Forbidden The request was a legal request, but the server is refusing to respond to it
404 Not Found Page Not Found
405 Method not allowed A request was made of a page using a request method not supported by that page
408 Timeout server time out

Here I will explain how to handle these errors and redirect the page to a friendly error page.
 
I have a website where I don't have any "students.aspx" page. When a new page is requested, it will provide me the following error message:
 
 
 
Since the page studentd.aspx does not exist, it will show the following error:
 
 
So this error page is very unattractive to show and the user does not understand what the is reason for this error message. So we need to provide a user-friendly message using a custom error setting.
 
Here the figure explains what I have explained:
 
 
 To set a custom use the following procedure:
 
 1. Go to web.config and add a tag called custom error like the following code snippet:
  1.  <customErrors mode="On">  
  2.    <error statusCode="404" redirect="error.aspx"/>  
  3. </customErrors> 
2. Create an error.aspx page and provide a user-friendly message there. The code is shown in the following image:
 
 
 Now when we request the same page, instead of giving the default error page it will provide our "error.aspx" page.
 
 
Now everyone will know that this page is not there. The following figure shows how it works:
 
 
When we are using custom error mode we have the following 3 options:
 
  • OFF: ASP.NET uses its default error page for both local and remote users in case of an error.

  • ON: Custom error will be enabled for both local as well as remote client.

  • REMOTE ONLY: The custom error page will be shown to a remote user only, the local user will get the built-in error page.

Up Next
    Ebook Download
    View all
    Learn
    View all