5,533,615 members and growing! (17,504 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The Code Project Open License (CPOL)

custom error page

By aresguya

step by step guide in creating a custom error 500 page
C#, Windows, .NET, ASP.NET, Dev

Posted: 30 Sep 2007
Updated: 30 Sep 2007
Views: 3,983
Bookmarked: 3 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.05 Rating: 1.50 out of 5
4 votes, 80.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
1 vote, 20.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

"Your Web server encountered an unexpected condition that prevented it from fulfilling the request by the client"

Security is an important part of web applications that require sensitive data to be passed and most often than not server errors occur, this shows a mistake in the part of the programmer.

By default asp.net shows a detailed error page, specially with the trace on. this makes your web application prone to hacks. to avoid this, one option is to create a custom error page.

Using the code

first thing is to edit the web.config file and turn on custom errors.

customErrors has 3 settings however i have found that the "on" mode works best when deploying an application. insert the following code between <System.web> and </system.web> as follows:

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
 <system.web>
        <customErrors mode="On">
              <error statusCode="500" redirect="ErrorPage.aspx"/>
        </customErrors>    
  </system.web>
</configuration>

For the custom page. create a webpage, it can be html or aspx. place your error message.

i always like to go for the following code:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Error Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
 <h1>The page cannot be displayed</h1>
There is a problem with the page you are trying to reach and it cannot be displayed.
<hr>
<p>Please try again later:</p>
<ul>
<li>An e-mail has been sent to the site owner to report the problem.</li>

</ul>
    </div>
    </form>
</body>
</html>

you can type anything.

now continue building your website. anytime you get a server error. the page will be shown.

during development its best so set the mode to "off".

Points of Interest

in a big application that requires security showing less means showing just enough.

this way of creating custom error pages can be done for most http errors.

please check http://modemhelp.net/httperrors/httperrors.shtml for a list of http errors.

a good practice would be to include a link so the user wont need to click the back button on the browser.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

aresguya


My name : Aresh Saharkhiz.
Origin: Unknown

Education : BS Computer Science
MS Computer Science
Interests : Genetic Programming
Neural Networks
Game AI
Programming: (language == statementEnd(semicolon)

Skill:
Flash
Carrara 3D
PHP,ASP,ASP.NET
J2SE
Occupation: Web Developer
Location: Philippines Philippines

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2007
Editor:
Copyright 2007 by aresguya
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project