views:

1141

answers:

2

Setting Response.StatusCode = 404 doesn't serve content under neither IE8 nor Chrome? It works in Mozilla though I find it strange!

Do the simplest of things - empty asp.net web application project with empty Default.aspx page. In Page_Load event use the following:

protected void Page_Load( object sender, EventArgs e )
{
    Response.StatusCode = 404;
}

This effectively sets the status code of the current request to 404, no doubt about that. When rendering under IE8 or Chrome, or may be some other browsers as well - I haven't tested, the actual page doesn't show up at all. These browsers display their default 404 error pages (NOT default IIS custom errors). Example in IE8:

The webpage cannot be found 
 HTTP 404  
   Most likely causes:
•There might be a typing error in the address.
•If you clicked on a link, it may be out of date. ... and so on ...

What I really want to do though is to serve 404 error page with 404 error code which will actually tell the browser or the crawler or whoever that this page doesn't exist - not only to show some fancy custom error message with status message 200 OK.

Using fiddler shows that I am actually really serving the request, but the browser is totally ignoring it?!

My question - how can I set 404 status code and still render page content? Example - http://www.intel.com/invalidpage.wow. Using fiddler shows that this page is served with 404 status code.

+1  A: 

You don't serve the content, you set a custom error page at the web server level (IIS) or in the web.config in the case of asp.net

Rich
I don't agree. Don't speak in general like this when you don't know the situation. In routing situations where the URL is constructed at runtime, and your Error page is actually virtual, you can't setup something in IIS when you don't know where it is. And one more thing, setting IIS to respond to 404 error with error page will not serve it under 404 status code. And your response is totally irrelevant to what I asked!
Ivan Zlatanov
+5  A: 

By default IE will show it's custom error page if the response for the error is less than a configurable amount. I believe the amount is 512 bytes, but I will try to find some confirmation on this. So all you need to do is put more content in your response.

EDIT: This blog post describes the limits. One of the comments shows the registry key settings for changing these values. The key is:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\ErrorThresholds

Daniel
More about that here: http://weblogs.asp.net/scottgu/archive/2006/04/09/App_5F00_Offline.htm-and-working-around-the-_2200_IE-Friendly-Errors_2200_-feature.aspx
Jon Galloway
Great help! Exactly the thing I was looking to understand. Thanks.
Ivan Zlatanov