So you created an ASP.NET application, hit a 404 Not Found error, and now you want to show something else other than the ugly default ASP.NET / IIS error pages, right? Well, sit back, because it’s rather annoying to setup properly. I strongly suggest reading the details that encouraged me to write this article. While that article definitely provides a majority of the setup required to make sure all cases and edge cases are handled when errors occur, there are some things that I had to do in order to make it all work properly.
Create a .html file and .aspx file for each error you want to handle. For example, 404.html and 404.aspx.
In the .aspx page, add this line below the “<%@ Page %>” directive where “xxx” is the error code you are handling such as 404 or 500.
<% Response.StatusCode = xxx; %>
In web.config, add this to your system.web section where each <error> section relates to whatever codes you are handling.
<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx">
<error statusCode="404" redirect="~/404.aspx" />
In web.config add this to your system.webServer section where each <remove> and <error> section relates to whatever codes you are handling.
<error statusCode="404" path="404.html" responseMode="File" />
Be sure to read through the originally linked blog to understand why you have to do certain things. I will say, however, that I had to fix a few things:
- There was a missing semi-colon in the .aspx line for “Response.StatusCode”.