The 404 page in web.config

by edward.lategan October 19, 2009 11:30

What is a 404 page? According to Wikipedia the 404 page is a HTTP standard response code indicating that the client was able to communicate with the server but the server could not find what was requested. This could be a static page like "aboutme.aspx" or a dynamic content page like "default.aspx?page=phoenixcode".

In ASP.NET you have the option to create your own 404 page, and redirect the user to this page if the page does not exist, and not display an ugly error to the user.

If you would configured custom 404 page in web.config file in the "customErrors" section, this will tell the server to serve your 404 page, instead of showing an ugly message to the user.

   1:  <customErrors defaultRedirect="ErrorPage.aspx" mode="On">   
   2:      <error statusCode="404" redirect="404.aspx" />
   3:  </customErrors>


The code snippet above will send all users to a "ErrorPage.aspx" page if there is an error in the application, but if there is a 404 error, the user will be redirected to "404.aspx" instead.

You also don't need to parse the customErrors section to get name of the custom 404 page. Just throw HttpException:

   1:  throw new HttpException(404, "Article not found");
   
ASP.NET run-time will catch the exception and will redirect to the custom 404 page.

Tags: , , ,

C# | General

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

About Me

Jacobus Meintjes
 
C# developer working with ASP.Net and/or Windows Forms.
Email Me

Cumulus

This will be shown to users with no Flash or Javascript.

Widget Twitter not found.

Root element is missing.X