ASP.NET MVC 404 处理和 IIS7 <httpErrors> [英] ASP.NET MVC 404 handling and IIS7 <httpErrors>

查看:38
本文介绍了ASP.NET MVC 404 处理和 IIS7 <httpErrors>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我使用策略来处理这样的 404 错误:ASP.NET MVC 2 和 IIS 7.0 的错误处理 或者这个:如何正确处理 ASP.NET MVC 中的 404?

I use strategy to handle 404 errors like this: Error handling for ASP.NET MVC 2 and IIS 7.0 or this: How can I properly handle 404 in ASP.NET MVC?

简而言之:我在 Global.asax 中将 404 作为异常处理,而不添加任何路由规则,如果异常是 404,我会呈现带有错误消息的特殊控制器操作.

In short: I handle 404 as exception in Global.asax without adding any routing rules, if the exception is 404 I render special controlleraction with error message.

在 IIS6 上,它适用于 ASP.NET 通配符映射.在集成模式的 IIS7 上,我需要将以下内容添加到 Web.config (其中 /error/HttpError404 是我对 404 页面的操作):

On IIS6 it works with ASP.NET wildcard mapping. On IIS7 in integrated mode I need to add the following to the Web.config (where /error/HttpError404 is my action with 404 page):

    <httpErrors>
        <remove statusCode="403" subStatusCode="-1" />
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/error/HttpError404" responseMode="ExecuteURL" />
        <error statusCode="403" prefixLanguageFilePath="" path="/error/HttpError403" responseMode="ExecuteURL" />
    </httpErrors>

为什么?路由在 IIS 集成模式和 IIS6 通配符映射上的工作方式不同?

Why? Routing works differently on IIS integrated mode and IIS6 wildcard mapping?

提前致谢!

更新:根据我的测试,我的错误处理似乎有效并且我的 404 操作被渲染,但似乎 IIS 看到 404 响应代码(我在我的 404 操作中以编程方式设置)并用默认错误替换我的页面.

UPDATE: According to my tests it seems that my error handling works and my 404 action is rendered, but it seems that IIS sees 404 response code (which I set programmatically in my 404 action) and replace my page with default errors.

当我设置 <httpErrors> 时,我得到了两个 404 页面的命中:一个来自 Global.asax 中的异常处理,另一个来自 IIS.

When I set <httpErrors> I got two hits for 404 page: one from exception handling in Global.asax and one from IIS.

这可能是原因吗?

推荐答案

看来我已经通过 Web.config 中的设置修复了它:

It seems I have fixed it by setting in Web.config:

<httpErrors existingResponse="PassThrough" />

这篇关于ASP.NET MVC 404 处理和 IIS7 &lt;httpErrors&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆