ASP.NET改写自定义错误不发送Content-Type头 [英] ASP.NET rewritten custom errors do not send content-type header

查看:94
本文介绍了ASP.NET改写自定义错误不发送Content-Type头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的配置,我的web.config:

I have the following configuration in my 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应用程序。

FWIW, this is an ASP.NET MVC 3 application.

当我产生错误。例如访问。

When I generate an error. For example by visiting..

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

..这是ASP.NET请求验证堵塞,错误页面会返回,但没有Content-Type头。 IE推断的内容和呈现HTML,但是Firefix(正确IMO)把内容作为文本并显示HTML code。

.. which is blocked by ASP.NET request validation, the error page is returned, but there is no content-type header. IE infers the content and renders the HTML, however Firefix (correctly IMO) treats the content as text and displays the HTML code.

有没有我需要说服ASP.NET发送内容类型的报头额外的步骤?我认为这是关系到一个事实,即它正将文件从文件系统,但MIME类型似乎是在服务器上配置正确。

Are there additional steps that I need to take to persuade ASP.NET to send a content type header? I assume this is related to the fact that it's picking the files up from the file system, but the MIME types appear to be configured correctly on the server.

推荐答案

我的ASP.NET MVC 2应用程序发送Content-Type头 - 的Content-Type:text / html的 - 正确。然后,它开始从升级.Net框架v2中的应用程序池到.NET Framework 4版后,给这个奇怪的问题。我用下面的配置

My ASP.NET MVC 2 application was sending the content-type header - Content-Type: text/html - correctly. Then it started giving this weird problem after upgrading the application pool from .Net Framework v2 to .Net Framework v4. I'm using the following configuration

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

和我想坚持我的自定义错误页的静态页面。

and I wanted to stick to static pages for my custom error pages.

我能想到的唯一的解决办法是在的Global.asax.cs文件中MvcApplication类的Application_Error事件明确设置了头。

The only solution I could think of was setting the header explicitly in the Application_Error method of the MvcApplication class in the Global.asax.cs file.

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

一个有点讨厌但来到我的脑海里最简单的解决方案。

A bit annoying but the simplest solution that came to my mind.

这篇关于ASP.NET改写自定义错误不发送Content-Type头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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