title: ASP.NET MVC 3: Server cannot append header after HTTP headers have been sent
body: <p>We're busy upgrading an ASP.NET MVC 2 Application using the 3.5 framework to an ASP.NET MVC 3 Application running on the 4.0 framework.</p>
  
  <p>There's a page which throws an exception when approached by using the browser back button. To support the browser back button on this page we implemented a system that requests the results for that page anew when arriving back on the page. I have no clear indications where to look for the problem however since I always only find the error</p>
  
  <pre><code>Server cannot append header after HTTP headers have been sent.
  </code></pre>
  
  <p>With stacktrace</p>
  
  <pre><code>at System.Web.HttpResponse.AppendHeader(String name, String value)
  at System.Web.HttpResponseWrapper.AppendHeader(String name, String value)
  at System.Web.Mvc.MvcHandler.AddVersionHeader(HttpContextBase httpContext)
  at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController&amp; controller, IControllerFactory&amp; factory)
  at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass6.&lt;BeginProcessRequest&gt;b__2()
  at System.Web.Mvc.SecurityUtil.&lt;&gt;c__DisplayClassb`1.&lt;ProcessInApplicationTrust&gt;b__a()
  at System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f)
  at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
  at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func)
  at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
  at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
  at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>How come the HTTP headers already have been sent?</p>
  
  <p>Thank you in advance,
  IvanL</p>
  
  <p>EDIT:
  I am adding new information and insight I gained while hunting for this problem. The Asynch Controller mention of one of the answers got me wondering. When I found that I had to change the following for the old MVC2 method to work:</p>
  
  <pre><code>[HttpPost, ValidateInput(false)]
  public void SearchResultOverview(SearchResultViewModel model, string searchUrl)
  {
      if (!string.IsNullOrEmpty(searchUrl))
      {
          searchUrl = searchUrl.Replace("SearchPartial", "SearchPartialInternal");
  
          //NOTE MVC 3
          HttpContext.Server.TransferRequest(searchUrl, true);
  
          //NOTE MVC 2
          //System.Web.HttpContext.Current.RewritePath(searchUrl, false);
  
          //IHttpHandler httpHandler = new MvcHttpHandler();
          //// Process request
          //httpHandler.ProcessRequest(System.Web.HttpContext.Current);
      }
  }
  </code></pre>
  
  <p>When I looked up the TransferRequest method I found that it <code>Performs an asynchronous execution of the specified URL and preserves query string parameters.</code> ( <a href="http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transferrequest.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transferrequest.aspx</a> )</p>
  
  <p>Also there is an Exception being thrown before the exception I posted (I simply missed out on it since I cought the exception late). This exception is:</p>
  
  <pre><code>The SessionStateTempDataProvider class requires session state to be enabled.
     at System.Web.Mvc.SessionStateTempDataProvider.SaveTempData(ControllerContext controllerContext, IDictionary`2 values)
     at System.Web.Mvc.TempDataDictionary.Save(ControllerContext controllerContext, ITempDataProvider tempDataProvider)
     at System.Web.Mvc.Controller.PossiblySaveTempData()
     at System.Web.Mvc.Controller.ExecuteCore()
     at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
     at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
     at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass6.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
     at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d()
     at System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f)
     at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
     at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
     at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
     at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>So how do I make this work?</p>