HttpException: Server cannot modify cookies after HTTP headers have been sent.

Problem

I have the following code on a page in a project that runs in a virtual directory under Community Server 2.x.

   92   Page.Response.Buffer = false;
   93 
   94   Response.Clear();
   95   Response.ContentType = "application/pdf";
   96   Response.AddHeader("content-length", fileArray.Length.ToString());
   97   Response.AddHeader("Content-Disposition", "inline; filename=application.pdf");
   98   Response.BinaryWrite(fileArray);
   99   fileArray = null;
  100   Response.End();

Whenever I go to this page, execute the code and then go to just about any other page on the site, I get the following error:

 [HttpException (0x80004005): Server cannot modify cookies after HTTP headers have been sent.]
   System.Web.HttpCookieCollection.Add(HttpCookie cookie) +103
   System.Web.Security.RoleManagerModule.OnLeave(Object source, EventArgs eventArgs) +412

I am not sure if this has anything specifically to do with Community Server, but I saw this error referenced in a Community Server forum post while searching for the solution.

Solution

This MSDN forum post eventually lead me to the solution.

Simply turning on buffering resolved my problem. So now the above code should read: Page.Response.Buffer = true;

Published Monday, June 04, 2007 3:12 PM by Tod Birdsall

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit