gpt4 book ai didi

.net - 使用 IHttpHandler 时,Firefox 无法正确处理内容类型

转载 作者:可可西里 更新时间:2023-11-01 16:35:25 24 4
gpt4 key购买 nike

由于某种原因,Firefox 无法正确处理通过我的 .NET HTTP 处理程序发送的内容;它似乎不遵守内容类型 header 。相反,它将内容视为 HTML。映射到请求的 URL 甚至具有 .csv 扩展名。 Internet Explorer 和 Chrome 正在做正确的事。 “text/css”和“application/pdf”处理程序都会出现问题。

这是我的 HTTP 处理程序的 ProcessRequest 方法的片段:

public void ProcessRequest(HttpContext context)
{
// ...

// Set the output headers
context.Response.ClearHeaders();
context.Response.ContentType = "text/csv";
context.Response.AddHeader(
"Content-Disposition", "attachment; filename=foo.csv");

// Code that writes to the output stream
// ...

context.Response.End();
}

我的响应中缺少什么能让 Firefox 按预期识别内容类型?

编辑 1:

当使用 Firefox Live HTTP Headers 扩展时,我看到,我得到了以下 header 。看起来我的 ContentType header 丢失了。

HTTP/1.x 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: Thu, 31 Dec 2009 02:34:09 GMT
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment;filename="foo.csv"
Cache-Control: private
Content-Type: text/html
Content-Length: 66682
Connection: Close

编辑 2:

发现问题。在我的处理程序中,我使用 context.Server.Execute 从 ASPX 模板生成 HTML,然后处理该 HTML。换句话说,我没有使用 context。 Server.Execute 直接输出到响应。尽管如此,运行该方法会修改当前上下文的响应 header 。所以这是在撤消我设置的标题。将修改 header 的代码移动到 context.Server.Execute 之后解决了这个问题。

这只影响 Firefox 的原因是因为其他浏览器使用文件扩展名而不是内容类型。 Firefox 做正确的事。

最佳答案

这看起来确实很奇怪。我会安装 Live HTTP Headers Firefox 的附加组件只是为了确认 Firefox 确实如您所料看到了这两个 header 。

RFC 2616 似乎也建议 placing quotes round the filename ,所以您也可以尝试一下。

关于.net - 使用 IHttpHandler 时,Firefox 无法正确处理内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1982909/

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