代码之家  ›  专栏  ›  技术社区  ›  Jacob

使用ihtpHandler时,火狐无法正确处理内容类型

  •  2
  • Jacob  · 技术社区  · 15 年前

    由于某种原因,Firefox无法正确处理通过.NET HTTP处理程序发送的内容;它似乎没有遵循内容类型的标题。相反,它将内容视为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扩展时,我看到了,我正在返回下面的头。看起来我的ContentType头丢失了。

    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 直接输出到响应。尽管如此,运行该方法modify是当前上下文的响应头。所以这是在撤消我设置的标题。将修改头的代码移到后面 context.server.execute 解决了问题。

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

    1 回复  |  直到 15 年前
        1
  •  2
  •   Christopher Orr    15 年前

    这似乎很奇怪。我会安装 Live HTTP Headers 火狐的附加组件只是为了确认火狐确实看到了这两个头部,正如您所期望的那样。

    RFC2616似乎也建议 placing quotes round the filename 所以你也可以试试。