views:

15

answers:

1

I am attempting to set the content-type of an asp.net .ashx file to text/plain.

When I run this through the ASP.NET Development Server, the content-type is properly set. When I serve it through IIS7, however, the content-type (and any other header values I set) don't come through (it came through as text/html).

The only value set in the HTTP Response Headers section of IIS Manager is the X-Powered-By attribute. I tried setting the content-type here, but that didn't work. But if I removed the X-Powered-By attribute, it was removed from the header.

Any ideas?

Code in .ashx file

public class Queries1 : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("hello");
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

HTTP Header from IIS7 (pulled through python script):

[('content-length', '58'), 
('x-powered-by', 'ASP.NET'), 
('server', 'Microsoft-IIS/7.0'), 
('date', 'Thu, 21 Oct 2010 15:51:28 GMT'), 
('content-type', 'text/html'), 
('www-authenticate', 'Negotiate, NTLM')]
A: 

To add HTTP Headers you need to use:

context.Response.Headers.Add("MyHeader", "Hello World!");

Based on Coding Gorilla's clarification, are you sure you're browsing to the correct url? If I try the exact same code as you've written I see the following in Fiddler:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Thu, 21 Oct 2010 20:11:44 GMT
Content-Length: 5

hello
Kev
I don't think his example here is about setting the headers, although he did mention that, I think his cheif complaint is that he's changing the content type and it's not having any effect.
Coding Gorilla
You are right. I had the write url, but didn't connect that the site requires authentication, and so a simple GET wasn't returning the page I thought it was. My mistake. Thanks for the help.
Trent
@Trent - ah right. That was the next thing I was going to ask, if you were authenticating, when saw the `'www-authenticate', 'Negotiate, NTLM`.
Kev