Click here to Skip to main content
13,346,397 members (131,232 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 10 Oct 2011

Custom error pages for ALL file types

, 10 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Configure your IIS site to serve custom error pages for all your file types: .html, .aspx, .pdf, whatever.
While trying to set up a custom 404 page for my company website, I found a lot of conflicting information. Use one method, and your .aspx pages were handled correctly while other file types still used the IIS default page. Use a different method, and everything BUT .aspx pages got your custom message. I spent several hours trying to figure out what I was doing wrong. When the blindingly obvious solution hit, it was physically painful. The answer?

Use both methods.

By design, Internet Information Server suffers from multiple personality disorder, by way of "handler mappings." This allows the web admin to direct http requests through different pre-processing filters based on their file extension. Most requests are handled by a chain of default handlers that decide if the request is a file or a directory and respond accordingly. Requests for .aspx files are handled by a special module that goes through the page life cycle; processing that is irrelevant for a .htm or .gif file. Each of these types of request look at different parts of the web.config for how to treat errors.

ASP.Net pages -- .aspx files and, I think (untested) old-style .asp -- are handled under the <system.web> block:

  <customerrors mode="On">
    <!-- ASPX pages -->
    <error statuscode="404" redirect="~/errors/404.aspx" />

This tells the ASP.Net handler module to use custom errors and, when a code of 404 is generated, redirect the user to a specific page.

All other files that are handled by the default handler chain require a different setting in the <system.webServer> block:

  <httperrors errormode="Custom">
    <!-- Non-ASPX pages -->
    <remove statuscode="404" substatuscode="-1" />
    <error statuscode="404" prefixlanguagefilepath="">
          path="/errors/404.aspx" responseMode="ExecuteURL" />

The <remove /> line tells IIS to remove the default handler for 404; the -1 sub-status means (as far as I can tell) remove all handlers if there are different ones for different sub status codes. The <error /> inserts the new custom behavior.

The difference, as far as I can tell, is to support different behaviors for ASP and non-ASP pages. In any case, if you want to provide a custom error page regardless of what the user requests, you will need to put both of these blocks into your web.config file. You can, of course, use these for any error status such as 401 (Unauthorized), 403 (Forbidden) or 500 (Internal error).

I hope this helps.


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


About the Author

Gregory Gadow
Software Developer (Senior)
United States United States
Gregory Gadow is currently a student at Central Washington University, studying for a B.S. in Actuarial Science with plans to graduate in June, 2019. He has been writing code for almost 30 years in more than a dozen programming languages. Previously, he worked for a mid-size brokerage firm as the company's programming department doing VB6, VB.Net, ASP, HTML, XML and SQL. He also held the Series 7 and Series 66 brokerage licenses.

You may also be interested in...


Comments and Discussions

QuestionCustom Error 404 Not Working when running the Website on IIS 8. Pin
memberANKIT PRAJAPATI7-Feb-13 18:43 
AnswerRe: Custom Error 404 Not Working when running the Website on IIS 8. Pin
Gregory.Gadow12-Feb-13 5:00
memberGregory.Gadow12-Feb-13 5:00 
AnswerRe: Custom Error 404 Not Working when running the Website on IIS 8. Pin
ANKIT PRAJAPATI27-Feb-13 20:25
memberANKIT PRAJAPATI27-Feb-13 20:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 10 Oct 2011
Article Copyright 2011 by Gregory Gadow
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid