views:

116

answers:

2

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"&gt;

<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

A: 

Your problem is probably that you have tried to set the Status Code (which counts as part of the Header) at some point after you've starting sending the Body of the response i.e. your pages contents - in your case the message.

To solve this you can try setting Response.Buffer to true and then if you have to set a 404/500 response code then call Response.Clear() before setting the response code.

Note that if you are sending a 404/500 then there should generally be no body to the response (although the HTTP spec does allow for it)

RobV
i set the StatusCode @ OnLoad (where I contact my Web Service). Output of the page is generelly made in the Render method. I do not want to clear my initial response, which is a normal result page, just with a different message.
Andreas Niedermair
have you tried just turning on Response.Buffer (but not clearing the response) and seeing if that works?
RobV
nope - I set this.Response.Buffer to true, did not help... I do never clear my response - just set the StatusCode.
Andreas Niedermair
+2  A: 

Hello,

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)

najmeddine
hehe ... nope - not yet ... i will give it a try tomorrow!
Andreas Niedermair
works like charm!
Andreas Niedermair
glad for you!
najmeddine