Custom Error Handling With Extension in ASP.Net

Introduction

Showing an error on a webpage like “This web page is not available”, “error 404” or “Server error in ‘/’” does not make a good impression. Sometimes users enter the wrong URL or extension to open the page and when that generates an error the user might become irritated. So to avoid this problem we can show a custom message on the web page instead of “This web page is not available” or “Server error in ‘/’”. When we show a custom message on the page the user will understand that they entered the wrong URL or extension and I think it's very helpful to make the user friendly.

Explanation

We can show a custom error message on a page using a few steps and a few lines of code. The following is the procedure.

Step 1
 
Get a Default.aspx page.

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <p>  
  12.             This is our home page.  
  13.         </p>  
  14.             </div>  
  15.         </form>  
  16.     </body>  
  17. </html>  

Step 2

Get an error.aspx page for showing the custom error message.

error.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="error.aspx.cs" Inherits="error" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <p> sorry for interruption......</p>  
  12.             </div>  
  13.         </form>  
  14.     </body>  
  15. </html>  

Step 3

Get an error404.aspx page for showing a 404 error message.

error404.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="error404.aspx.cs" Inherits="error404" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <p>Lost your way. Please go to home page. </p>  
  12.             </div>  
  13.         </form>  
  14.     </body>  
  15. </html>  

Step 4

Open the web.config file and create or modify the two sections <system.web> and <system.webserver>. Using the <system.webserver > section we specify settings for IIS 7.0 that are applied to the web application.

Attributes

mode: This specifies whether custom errors are enabled or disabled. There are the following three types of custom errors in ASP.Net.

  1. On: Its specifies that custom errors are enabled. If no defaultRedirect is specified, users see a generic error.
  2. Off: Specifies that custom errors are disabled. This allows the display of detailed errors.
  3. RemoteOnly: Specifies that custom errors are shown only to remote clients and ASP.NET errors are shown to the local host. This is the default.

defaultRedirect: Its specifies the default URL to direct a browser to if an error occurs.

httpErrors errorMode: Its specifies the error code like 404, 501 and so on.

Web.config

  1. <?xml version="1.0"?>  
  2. <!-- 
  3. For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 
  4. -->  
  5. <configuration>  
  6.     <system.web>  
  7.         <customErrors mode="On" defaultRedirect="error.aspx"></customErrors>  
  8.         <compilation debug="false" targetFramework="4.5" />  
  9.         <httpRuntime targetFramework="4.5" />  
  10.     </system.web>  
  11.     <system.webServer>  
  12.         <httpErrors errorMode="Custom">  
  13.             <remove statusCode="404" subStatusCode="-1"/>  
  14.             <error statusCode="404" prefixLanguageFilePath="" path="error404.aspx" responseMode="Redirect"/>  
  15.         </httpErrors>  
  16.         <modules runAllManagedModulesForAllRequests="true"/>  
  17.     </system.webServer>  
  18. </configuration>  

When an error occurrs due to an URL user redirect of a page then error.aspx or error404.aspx is used.

This way we can create custom error pages in ASP.Net. This is my first article so please provide suggestions for me of where I need to improve.


Similar Articles