ASP.NET重写自定义错误不发送内容类型 header

标签 asp.net

我的 web.config 中有以下配置:

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/Error.html">
    <error statusCode="404" redirect="~/Error/Error.html" />
    <error statusCode="400" redirect="~/Error/Error.html" />
</customErrors>

FWIW,这是一个 ASP.NET MVC 3 应用程序。

当我生成错误时。例如通过访问..

http://testserver/this&is&an&illegal&request

.. 被 ASP.NET 请求验证阻止,返回错误页面,但没有内容类型 header 。 IE 推断内容并呈现 HTML,但是 Firefix(正确的 IMO)将内容视为文本并显示 HTML 代码。

我需要采取其他步骤来说服 ASP.NET 发送内容类型 header 吗?我认为这与它从文件系统中获取文件有关,但 MIME 类型似乎在服务器上配置正确。

最佳答案

我的 ASP.NET MVC 2 应用程序正确发送了内容类型 header - Content-Type: text/html。然后,在将应用程序池从 .Net Framework v2 升级到 .Net Framework v4 后,它开始出现这个奇怪的问题。我正在使用以下配置

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/500.html">
    <error statusCode="404" redirect="/404.html" />
</customErrors>

我想坚持使用静态页面作为我的自定义错误页面。

我能想到的唯一解决方案是在 Global.asax.cs 文件中 MvcApplication 类的 Application_Error 方法中显式设置 header 。

public class MvcApplication : System.Web.HttpApplication
{
    // .
    // .
    // .
    void Application_Error(object sender, EventArgs e)
    {
        // .
        // Remember to set Response.StatusCode and
        // Response.TrySkipIisCustomErrors
        // .
        Response.ContentType = "text/html";
        // .
        // .
        // .
    }
    // .
    // .
    // .
}

有点烦人,但我想到的最简单的解决方案。

关于ASP.NET重写自定义错误不发送内容类型 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418021/

相关文章:

asp.net - ASP.NET 中使用了哪个 session 超时?

c# - OpenXML - 保护所有工作表

c# - asp.net 中的文件处理程序

asp.net - WebResource.axd 中的 AbsoluteUrl

c# - 在 AspNet WebApi 帮助页面中生成模型描述

c# - 将原始帧转换为 webm 实时流

c# - 使用 ASP.NET MVC 的 ViewBag foreach 循环中图像滑动不起作用

asp.net - DropDownList 内容出现在引擎搜索结果中

C# facebook graph/如何在 Facebook 相册上上传图片?

c# - asp.net mvc 5.1 mysql Entity Framework Controller 生成