gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:59:00 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com