Creating custom error pages in ASP.Net applications

This is going to be another short post regarding ASP.Net and custom error pages. Our goal is to specify a particular page to be displayed on the user's screen when error occurs within our application.In order to that we basically have to make some changes in the web.config file.

1) Launch Visual Studio and create a new ASP.Net web application from the available projects. 

2) Let's create the generic error page. Add another item to your project , an .htm page. I have named it GenericError.htm

Add some markup on the page

 <h2>There has been an error....</h2>
    <p>There was a problem processing your request.
    you can try later or contact support
    <a href="http://www.thesupport.com"</a>
  </p>

3) Let's create another custom error page for files not found on the server. Add another item to your project , an .htm page. I have named it notfound.htm

I have added some simple markup 

<p>We are sorry but the page you requested is not found in our server!!!!</p>

4) Now let's make the necessary adjustments to the web.config file.We have to add a customErrors element.

 <customErrors defaultRedirect="GenericError.htm" mode="On">
          <error statusCode="404" redirect="notfound.htm"/>
      </customErrors>

We have  various options we can set to the mode attribute. We can set it to On which means that the custom error pages will be always displayed.

Then there is the Off option which means we always display to everyone ASP.Net error pages. That is not the best option...It reveals far too much information about the error to people who may have the knowledge to harm our application.The third option and the one I use is RemoteOnly which means that ASP.Net error pages will be shown only when I am on the web server and hit an error and custom error pages will be shown to the remote clients.

5) Add another page to your application, a web form. Name it Exception.aspx

Add a button on your web page and leave the default name. In the Click() event handling routine type

        protected void Button1_Click(object sender, EventArgs e)
        {
             throw new ApplicationException("I am an exception!");
        }

 

6) Run your application and hit the button to generate an exception/error. You will see the contents of the GenericError.htm

If you type in the address bar  http://localhost:33173/Exceptionnnnnn.aspx instead of http://localhost:33173/Exception.aspx you will see the contents of the notfound.htm

7) Now let's change the web.config a bit.
 <customErrors defaultRedirect="GenericError.htm" mode="Off">
          <error statusCode="404" redirect="notfound.htm"/>
      </customErrors>
Run your application and hit the button to generate an exception/error. You will not see the contents of the GenericError.htm  but the ASP.Net error page.

8) Now let's change the web.config a bit.
 <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
          <error statusCode="404" redirect="notfound.htm"/>
      </customErrors>

Run your application and hit the button to generate an exception/error. You will not see the contents of the GenericError.htm  but the ASP.Net error page.

When I upload the page on a remote web server and try to access the page as a remote client, I will see the contents of the GenericError.htm  and not the ASP.Net error page.

This is the best setting(mode="RemoteOnly") in my mind  for someone to set.

Hope it helps!!!

1 Comment

Comments have been disabled for this content.