Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 867
Ivan Zlatanov Avatar asked Sep 24 '09 20:09

Ivan Zlatanov


People also ask

What is the most common reaction to HTTP status code 200?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.

Which value of status is equivalent to OK?

OK indicates that the request succeeded and that the requested information is in the response. This is the most common status code to receive. Equivalent to HTTP status 206.


1 Answers

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

like image 148
Daniel Avatar answered Oct 04 '22 22:10

Daniel