R – Setting ResponseCode manually at CodeBehind fails with IIS7 and 2008 Server

asp.netiis-6iis-7windows-server-2008

I have developed a web app a year ago aimed to work with IIS6.
Now we are moving to IIS7 and I thought, I'd do some integration tests.

One of this fails:
The web app is more or less a search-engine, giving a 404 or 500 (thanks to your google-advisor …) when there weren't any results or the data-container is not loaded yet. With IIS6 this worked great: The page output was eg. result.aspx, showing some message and giving back the specified http status (set at codebehind).
Now with IIS7 this behaviour is broken: If I set the http status code at codebehind, my page won't be delivered anymore – instead showing the generic error page of IIS7.

No, I do not want to do any dirty hack with the customErrors-Section …
I just want the original behaviour back!
Is there any way to do this?

Edit:
Consider following page

<%@ Page Language="C#" AutoEventWireup="true"%>

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        this.Response.StatusCode = 404;
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        This page should be displayed
    </div>
    </form>
</body>
</html>

Vista + IIS7 = OK
2008 Server + IIS7 = Generic Error Page

Best Answer

have you tried this:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    this.Response.StatusCode = 404;

    Response.TrySkipIisCustomErrors = true;
}

HttpResponse.TrySkipIisCustomErrors Property (System.Web)

Related Topic