ASP.NET custom 404 returning 200 OK instead of 404 Not Found

ASP.NET provides a simple yet powerful way to deal with errors that occur in your web applications. We will look at several ways to trap errors and display friendly meaningful messages to users. We will then take the discussion a step further and learn how to be instantly notified about problems so you can cope with them right away. As a geek touch we will also track the path 404's travel.

1. Edit the web.config for custom errors:
<customErrors mode="On">
  <error statusCode="404" redirect="~/404.aspx"/>
</customErrors>
2. Add a 404.aspx page and set the status code to 404.
public partial class _404 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404;
    }
}
Now, if I go to a page extension that is processed by ASP.NET and does not exist, my "Live HTTP header" addon in Firefox shows a 404. Here is the header:
HTTP/1.1 404 Not Found
Server: Microsoft-IIS/5.1
Date: Sun, 07 Dec 2008 06:04:13 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 533

Commentaires