c# - IIS 将自定义错误页面作为纯文本提供,没有内容类型 header

标签 c# iis asp.net-mvc-4 error-handling

UPD:这是 full solution for error handling

我有普通的 vanilla MVC4 网络项目。没有添加任何内容,也没有删除任何内容,只是在 Visual Studio 中创建了一个新项目。

web.config 中,我添加了自定义错误页面处理程序:

<customErrors mode="On" defaultRedirect="~/Content/Error.htm" redirectMode="ResponseRewrite" />

~/Content/Error.htm 文件是:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>OOPS! Error Occurred. Sorry about this.</title>
</head>
<body>
    <h2>OOPS! Error Occurred</h2>
</body>
</html>

每当我在网站上遇到 404 错误时,Error.htm 在 Firefox 和 Chrome 中作为明文提供:

HTML is displayed as plain text

Fiddler 表示错误页面在没有 content-type header 的情况下提供,这导致浏览器将页面呈现为纯文本:

No Content-type header

有没有办法强制使用 content-type header 的 IIS 服务器错误页面?

附注实际问题出在一个复杂的 MVC4 项目中,它在 Global.asax 中有自己的错误处理。但我发现有些错误不会通过 ASP 管道,而只能由 IIS 处理。喜欢dot at the end of the url .解决方案 < httpErrors />确实提供正确的响应,但我们在 Global.asax 中的自定义错误处理,Application_Error() 没有以这种方式被调用。

UPD 看来我赢不了这场 war 了。 IE 显示正确呈现的 html,Firefox 和 Chrome 显示为纯文本。当我切换到纯文本时,Firefox 和 IE 正确显示空白,IE 吞下空白并尝试呈现 html。如果我尝试将图像作为错误页面提供,Firefox 和 Chrome 会显示图像。 IE显示这个: IE9 is on crack! 打脸!

最佳答案

错误页面使用 .aspx 而不是 .htm(将 htm 重命名为 aspx)。

<customErrors mode="On" defaultRedirect="~/Content/Error.aspx" redirectMode="ResponseRewrite" />

关于c# - IIS 将自定义错误页面作为纯文本提供,没有内容类型 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384148/

相关文章:

c# - 使用 ViewRenderer 在 SignalR 响应中渲染 PartialView?

c# - 是否可以注册多点文件扩展名?

c# - John Resig 的微模板框架在 asp.net 网页上抛出错误

html - 更改 index.html 的内容是否需要重新启动 IIS 站点?

asp.net-mvc - <添加程序集=“System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35”/>

c# - 如何使请求主体可选

c# - EF Code First 外键必须映射到一些在概念方面参与外键关联的 AssociationSet 或 EntitySet

iis - 重写 IIS 7 中响应头的规则(替换 cookie 路径)

azure - Umbraco 5 与 MVC4 和 Azure

c# - 使用三元运算符检查 null - null 引用是否与 null 不同