How to programmatically set Response.StatusCode different than 200 OK in ASP.NET Application and still serve content to client successfully?

StackOverflow https://stackoverflow.com/questions/1473855

Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top