开发者

IIS7 overwriting defined HTTP header values

开发者 https://www.devze.com 2023-01-20 15:27 出处:网络
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

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 conte开发者_JAVA技巧nt-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')]


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
0

精彩评论

暂无评论...
验证码 换一张
取 消