Setting Context.Response.StatusCode does not seem to work

First Try this:

protected void Page_Load(object sender, EventArgs e)
{
    Response.StatusCode = 404;
    Response.SuppressContent = true;
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

:)~


Firebug shows the correct status. Does this mean that if I want the browser to display a message, I have to render it myself? – deverop

Absolutely it does. What the browser does based on an error code received is up to the browser. But you can still provide HTML to go along with the 404. Case in point... take a look at Stack Overflow's 404 page. That error message is entirely hand crafted.

Typically, however, you want to limit the amount of data returned from an error status; the more data you return from an erroneous request, the larger the surface of attack for denial of service.


I had a similar problem, which occures in IIS 7.0 only. What you could also try is to set

Response.TrySkipIisCustomErrors = true;