views:

300

answers:

1

Firefox is for some reason not properly handling content sent through my .NET HTTP handler; it appears to be not honoring the content type header. Rather, it sees the content as being HTML. The URL mapped to the request even has a .csv extension. Internet Explorer and Chrome are doing the right thing. The problem is happening for both a "text/css" and an "application/pdf" handler.

Here's a fragment of my HTTP handler's ProcessRequest method:

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();
}

What's missing from my response that would enable Firefox to recognize the content type as expected?

Edit 1:

When using the Firefox Live HTTP Headers extension, I saw, I'm getting back the following headers. It looks like my ContentType header is getting lost.

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

Edit 2:

Found the issue. In my handler, I was using context.Server.Execute to generate HTML from an ASPX template, then processing that HTML. In other words, I was not using context. Server.Execute to directly output to the response. Despite this, running that method modify's the current context's response headers. So this was undoing the headers I had set. Moving the code that modifies the headers to after context.Server.Execute solved the problem.

The reason this only affected Firefox is because the other browsers use the file extension rather than the content type. Firefox does the right thing.

+1  A: 

That does seem odd. I would install the Live HTTP Headers add-on for Firefox just to confirm that Firefox is indeed seeing those two headers as you'd expect.

RFC 2616 also seems to suggest placing quotes round the filename, so you could try that out as well.

Christopher
Excellent advice. I installed Live HTTP Headers and saw that indeed I'm not getting back the right content-type header. I'll shortly add the headers I am getting in the response.
Jacob
Cool. Sorry I can't help further though as I don't know about the ASP.NET development server and why or where things could be getting overridden. Perhaps when deployed on IIS or another server rather than a dev server things will work properly?
Christopher
Unfortunately, the same thing is happening in IIS as well.
Jacob
Ok, one final ill-informed suggestion from me... ;) How about explicitly using the `AddHeader` method with "Content-Type", rather than using the `ContentType` property?
Christopher
I tried the `AddHeader` thing already, to no avail. I found the issue, by the way, and it was caused by code I didn't post in my question. I'll still mark your answer as accepted since it set me on the path to finding the solution.
Jacob
Nice.. glad it's sorted, and good work with editing the question. Sounds like the solution could come in quite handy for people.
Christopher