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

jam*_*con 17 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>
Run Code Online (Sandbox Code Playgroud)

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

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

http://testserver/this&is&an&illegal&request
Run Code Online (Sandbox Code Playgroud)

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

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

Bed*_*ges 6

我的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>
Run Code Online (Sandbox Code Playgroud)

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

我能想到的唯一解决方案是在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";
        // .
        // .
        // .
    }
    // .
    // .
    // .
}
Run Code Online (Sandbox Code Playgroud)

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


Dav*_*ker 1

ASP.Net 不会发送此 IIS 意愿。如果您希望 ASP.Net 发送它,请尝试添加 Error.aspx 并看看它是什么样子?

我在这里的意思是,您将被发送到一个 html 页面 - 除非您已连接 IIS 通过 aspnet 运行这些页面,否则它将正常提供服务。

如果添加新页面 error.aspx,您可以 a) 检查 ASP.Net 是否已解决您的问题,或者 b) 手动在其中添加标头。