IIS 7.5没有注意到MVC 3应用程序返回的404的customErrors

时间:2011-02-24 13:11:32

标签: asp.net-mvc-3 iis-7.5 custom-errors

我正在使用.NET 4.0集成管道应用程序池在IIS 7.5(Win 7 64位)上运行我的MVC 3应用程序(最近从2更新),并在web.config中进行以下设置:

<customErrors mode="On" defaultRedirect="~/Problem/Oops" redirectMode="ResponseRedirect">
    <error statusCode="404" redirect="~/Problem/NotFound" />
</customErrors>

如果控制器上的操作方法引发服务器异常并因此生成500错误代码,则会正确地将浏览器发送到默认重定向URL。

但是,如果我的操作故意通过HttpNotFound()返回HttpNotFoundResult,则会出现IIS 7.5 404.0错误页面,而不是我的web.config中指示的错误页面。

如果我输入了我的应用中不存在的网址,例如http://localhost/MyApp/FOO,那么我会看到web.config指示的网页。

任何人都有任何想法,为什么我在使用HttpNotFound()时没有被重定向到我的自定义404错误页面?

2 个答案:

答案 0 :(得分:6)

请尝试使用以下语法,而不是调用 HttpNotFound 并让我知道结果;)

throw new HttpException(404, "NotFound");

答案 1 :(得分:0)

您是否尝试过设置Response.TrySkipIisCustomErrors = true;

(见http://blog.janjonas.net/2011-04-13/asp_net-prevent-iis_75_overriding-custom-error-page-iis-default-error-page