Creating a custom 404 page to handle link redirection for ASP.NET web applications

For some time now, I've wanted to document how to setup a custom 404 page that can handle link redirection for an ASP.NET web application.  In my case, I just needed to customize the 404 behavior to handle custom redirects for a DotNetNuke site that I was setting up.  But, others might find these steps useful to handle broken links or other creative error handling behavior they want to build into their sites/applications.

Anyhow, to set this up the first step is to create your custom 404.aspx page.  In my case that page looked something like this:

<%@ Page Language="VB" runat="server" %>    if InStr(strURL, UCase("oldpage1.htm")) <> 0 then

<script language="vb" runat="server">

Sub Page_Load()

    Dim strURL, arpath, strFile

    strURL = UCase(request.servervariables("QUERY_STRING"))

    arPath = Split(strURL, "/")

    strFile = arPath(UBound(arPath,1))

        response.redirect ("/Events/Default.aspx")

    elseif InStr(strURL, UCase("oldpage2.htm")) <> 0 then

        response.redirect ("/Publications/Default.aspx")

    elseif InStr(strURL, UCase("ic-folder")) <> 0 then

        response.redirect ("/Site/Files/" & strFile)


        lblFileString.Text = strFile

    end if

End Sub




    <meta name="robots" content="noindex,nofollow">

    <title> - 404 Page Not Found</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<body bgcolor="#FFFFFF">

    <UserControl:Header runat="server" />

    <h2>Page Not Found (404 Error)</h2>

    <asp:panel id="pnlValid404" runat="server">

        <p><asp:label id="lblFileString" runat="server" ForeColor="#FF0000" /> could not be located.<p>


    <asp:panel id="pnlNotValid404" runat="server" Visible="False">

        <p>This page works only as a redirect when an ASPX page is not found.</p>




Within the above code you will notice that the first part of the aspx page consists of some logic.  This logic is what determines where the requestor is redirected to when they encounter a 404 error.  For example, if you had a page that existed, but has since been removed and there still happens to be a bunch of marketing materials referencing that page.  You would add to the if,elseif,else,then statement a line that might look like:

    elseif InStr(strURL, UCase("oldmarketingpage2.htm")) <> 0 then

        response.redirect ("/NewMarketing/Default.aspx")


The same would also be true for structure changes where you needed to modify entire paths as shown with the ic-folder portion of the statement.  Then at the end of the statement is any logic you want to put in for actual 404 errors that were not intentional.  After all, at the end of the day, you still need to tell a web site visitor that a page, link, etc. is invalid.

After creating this page, the next step is to name it something like 404.aspx and dump it into a folder called something like ServerErrors.  Next, you then need to open your sites/applications web.config and turn on custom errors by modifying the customErrors tag as shown here:

   <customErrors mode="On">

       <error statusCode="404" redirect="/ServerErrors/404.aspx" />


Notice that for all 404 errors we are now redirecting to the 404.aspx page that was created in the previous two steps.  Lastly, you will also need to modify the IIS site properties so that your custom 404 page is used for all 404 errors.  To do this, open the Internet Services Manager.  Then on your site, right mouse click, select Properties, and then go to the Custom Errors tab.  Next, find the 404 HTTP Error, select it, and edit its properties.  In the error mappings properties dialog box change the message type to URL and set the URL to /ServerErrors/404.aspx.

Done... you now finished setting up your very own custom 404 page.

If you like this, check out some other posts from Tyson:


Or if you want, you can also check out some of Tyson's latest publications:


Lastly, visit the Microsoft Subnet for more news, blogs, and opinions from around the Internet.  Or, sign up for the bi-weekly Microsoft newsletter(Click on News/Microsoft News Alert)

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2008 IDG Communications, Inc.

IT Salary Survey: The results are in