Ian Boyd
Ian Boyd

Reputation: 256641

How to Response.Write on IIS7.5?

i am trying to write out a response to the client:

response.StatusCode = (int)HttpStatusCode.BadRequest;
response.ClearContent();
response.Write(String.Format(
      "<!doctype html>"+CRLF+
      "<html>" + CRLF +
      "<head><title>{0}</title></head>" + CRLF +
      "<body><h1>{0}</h1>"+CRLF+
      "{1}"+CRLF+
      "</body>" + CRLF +
      "</html>", 
      response.Status, "The grob must be in the frobber."));
response.Flush();
response.End();

The code works fine when running on the localhost (Visual Studio (2010 (Windows 7 (Professional (64-bit))))) development Cassini web-server:

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 17 Jul 2012 15:56:42 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/html
Connection: Close

<!doctype html>
<html>
<head><title>400 Bad Request</title></head>
<body><h1>400 Bad Request</h1>
The grob must be in the frobber.
</body>
</html>

But when i deploy the web-site to Windows Server 2008 R2 running IIS7.5, the same code doesn't work:

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 17 Jul 2012 15:57:44 GMT
Content-Length: 11

Bad Request

How do i perform Response.Write from IIS?

Upvotes: 1

Views: 3043

Answers (3)

Bryan
Bryan

Reputation: 31

I ran into this exact issue & spent a few frustrating hours today noodling through it. In my case, I am sending potentially very large CSV data directly down to the client via Response.Write and flushing it every 3k records or so. It works great from my local environment, but failed when deployed to IIS 7.5 on a Windows 2008 server. No exceptions, just an empty CSV at the client.

Turns out Dynamic Compression was enabled for my application. It can be disabled via web.config directly, or in your IIS MMC interface. You can validate this by using your browser's tools to examine the headers of the response you're receiving.

Content-Encoding:gzip

Disable dynamic compression (not sure how/why it was enabled originally), and it worked fine. It might not be your issue, but it was mine. I fished around for most of the day on this one and didn't see it answered directly, so I figured I'd throw it out there. Also, the size of the result set didn't matter; a 5kb file & 3Gb file had the same problem.

Upvotes: 3

mafue
mafue

Reputation: 1878

By default, IIS 7 will change the response if the code is >= 400 http://msdn.microsoft.com/en-us/library/ms690497(v=vs.90).aspx

But you can change this in the httpErrors element of system.webServer.

<httpErrors existingResponse="PassThrough" />

EDIT: this will break CustomErrors, if you are using that. You might be better off returning a 400 without a response, then setting up web.config to redirect to a custom page for 400 errors.

<customErrors mode="On">
    <error statusCode="400" redirect="Frobber.htm" />
</customErrors>

Upvotes: 5

Simon Halsey
Simon Halsey

Reputation: 5480

Have you checked that asp.net is enabled on the server? You need to use the Windows server manager to check the features have been installed.

I've just tried your code on my server & despite what I assume is a typo around casing of response, it worked as expected.

Upvotes: 0

Related Questions