加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:33:27 所属栏目: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" //customErr
我的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请求验证阻止,返回错误页面,但没有内容类型的头文件. IE推断内容并呈现HTML,但是Firefix(正确的IMO)将内容视为文本,并显示HTML代码.

是否需要采取其他步骤来说服ASP.NET发送内容类型标题?我认为这是与文件系统选择文件相关的事实,但MIME类型似乎在服务器上正确配置.

解决方法

我的ASP.NET MVC 2应用程序正确地发送内容类型头 – 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方法中显式设置头.

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";
        // .
        // .
        // .
    }
    // .
    // .
    // .
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读