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
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <p>
- This is our home page.
- </p>
- </div>
- </form>
- </body>
- </html>
Step 2
Get an error.aspx page for showing the custom error message.
error.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="error.aspx.cs" Inherits="error" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <p> sorry for interruption......</p>
- </div>
- </form>
- </body>
- </html>
Step 3
Get an error404.aspx page for showing a 404 error message.
error404.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="error404.aspx.cs" Inherits="error404" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <p>Lost your way. Please go to home page. </p>
- </div>
- </form>
- </body>
- </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.
- On: Its specifies that custom errors are enabled. If no defaultRedirect is specified, users see a generic error.
- Off: Specifies that custom errors are disabled. This allows the display of detailed errors.
- 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
- <?xml version="1.0"?>
-
-
-
- <configuration>
- <system.web>
- <customErrors mode="On" defaultRedirect="error.aspx"></customErrors>
- <compilation debug="false" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5" />
- </system.web>
- <system.webServer>
- <httpErrors errorMode="Custom">
- <remove statusCode="404" subStatusCode="-1"/>
- <error statusCode="404" prefixLanguageFilePath="" path="error404.aspx" responseMode="Redirect"/>
- </httpErrors>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </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.