使用IIS7和2008 Server在CodeBehind上手动设置ResponseCode失败

And*_*air 0 asp.net iis-7 iis-6 windows-server-2008

我在一年前开发了一个旨在与IIS6一起使用的Web应用程序.现在我们转向IIS7,我想,我会做一些集成测试.

其中一个失败:
网络应用程序或多或少是一个搜索引擎,当没有任何结果或数据容器尚未加载时,给出404或500(感谢您的google-advisor ...).使用IIS6,这非常有效:页面输出是例如.result.aspx,显示一些消息并返回指定的http状态(在codebehind中设置).
现在使用IIS7时,这种行为被破坏了:如果我在代码隐藏时设置http状态代码,我的页面将不再被传递 - 而是显示IIS7的通用错误页面.

不,我不想对customErrors-Section进行任何肮脏的黑客攻击......我只想恢复原来的行为!
有没有办法做到这一点?

编辑:
考虑以下页面

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

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

man*_*nji 6

你试过这个:

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

    Response.TrySkipIisCustomErrors = true;
}
Run Code Online (Sandbox Code Playgroud)

HttpResponse.TrySkipIisCustomErrors属性(System.Web)