Error with shape being displayed as partial: "Server cannot append header after HTTP headers have been sent."

Topics: General
May 16, 2014 at 1:13 PM
This is related to NGM.OpenAuthentication functionality however I think it might be related to the specific 1.8 version because my functionality stopped working after the upgrade.

Basically I'm using an alternate for a view which is provided in the library. The view is called "OpenAuthLogon".

I'm trying to display the view as a partial (i tried both @Html.Partial("OpenAuthLogon") and @Display("OpenAuthLogon")) but i see a blank page and the following message in the log:

"Server cannot append header after HTTP headers have been sent."

Debugging shows that it is related to the AntiForgery token which needs to be set in the headers. However I think that because of either "Theming", "Caching" or perhaps something else like 'buffering' the page headers are already sent to the client.

Any help would be greatly appreciated!

Stack trace:
2014-05-16 12:55:49,278 [49] Orchard.Exceptions.DefaultExceptionPolicy - Default - An unexpected exception was caught
http://localhost/OrchardLocal/ System.Web.HttpException (0x80004005): Server cannot append header after HTTP headers have been sent.
at System.Web.HttpResponse.AppendHeader(String name, String value)
at System.Web.HttpResponseWrapper.AddHeader(String name, String value)
at System.Web.Helpers.AntiXsrf.AntiForgeryWorker.GetFormInputElement(HttpContextBase httpContext)
at System.Web.Helpers.AntiForgery.GetHtml()
at System.Web.Mvc.HtmlHelper.AntiForgeryToken()
at Orchard.Mvc.Html.HtmlHelperExtensions.AntiForgeryTokenOrchard(HtmlHelper htmlHelper) in c:\src\Orchard\Mvc\Html\HtmlHelperExtensions.cs:line 296
at Orchard.Mvc.Html.MvcFormAntiForgeryPost.Dispose(Boolean disposing) in c:\src\Orchard\Mvc\Html\MvcFormAntiForgeryPost.cs:line 14
at System.Web.Mvc.Html.MvcForm.Dispose()
at ASP._Page_Themes_Lanceng_Views_OpenAuthLogOn_cshtml.Execute() in c:\src\Orchard.Web\Themes\Lanceng\Views\OpenAuthLogOn.cshtml:line 66
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) in c:\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:line 151
at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.<>c__DisplayClass26.<>c__DisplayClass28.<Discover>b__15(DisplayContext displayContext) in c:\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:line 133
at Orchard.DisplayManagement.Descriptors.ShapeAlterationBuilder.<>c__DisplayClass3.<>c__DisplayClass5.<BoundAs>b__2(DisplayContext displayContext) in c:\src\Orchard\DisplayManagement\Descriptors\ShapeAlterationBuilder.cs:line 56
at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context) in c:\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:line 185
at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Execute(DisplayContext context) in c:\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:line 94
at Orchard.DisplayManagement.Implementation.DisplayHelper.ShapeExecute(Object shape) in c:\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 86
at Orchard.DisplayManagement.Implementation.DisplayHelper.Invoke(String name, INamedEnumerable1 parameters) in c:\\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 44
at Orchard.DisplayManagement.Implementation.DisplayHelper.TryInvoke(InvokeBinder binder, Object[] args, Object& result) in c:\\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 29
at CallSite.Target(Closure , CallSite , Object , Object )
at ASP._Page_Themes_Lanceng_Views_Layout_cshtml.Execute() in c:\\Layout.cshtml:line 204
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit) in c:\\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:line 151
at Orchard.DisplayManagement.Descriptors.ShapeTemplateStrategy.ShapeTemplateBindingStrategy.<>c__DisplayClass26.<>c__DisplayClass28.<Discover>b__15(DisplayContext displayContext) in c:\\src\Orchard\DisplayManagement\Descriptors\ShapeTemplateStrategy\ShapeTemplateBindingStrategy.cs:line 133
at Orchard.DisplayManagement.Descriptors.ShapeAlterationBuilder.<>c__DisplayClass3.<>c__DisplayClass5.<BoundAs>b__2(DisplayContext displayContext) in c:\\src\Orchard\DisplayManagement\Descriptors\ShapeAlterationBuilder.cs:line 56
at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context) in c:\\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:line 185
at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.Execute(DisplayContext context) in c:\\src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:line 94
at Orchard.DisplayManagement.Implementation.DisplayHelper.ShapeExecute(Object shape) in c:\\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 86
at Orchard.DisplayManagement.Implementation.DisplayHelper.Invoke(String name, INamedEnumerable
1 parameters) in c:\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 44
at Orchard.DisplayManagement.Implementation.DisplayHelper.TryInvoke(InvokeBinder binder, Object[] args, Object& result) in c:\src\Orchard\DisplayManagement\Implementation\DisplayHelper.cs:line 29
at CallSite.Target(Closure , CallSite , Object , Object )
at Orchard.Mvc.ViewEngines.ThemeAwareness.LayoutAwareViewEngine.<>c__DisplayClass7.<FindView>b__5(ViewContext viewContext, TextWriter writer, IViewDataContainer viewDataContainer) in c:\src\Orchard\Mvc\ViewEngines\ThemeAwareness\LayoutAwareViewEngine.cs:line 63
at Orchard.Mvc.ViewEngines.ThemeAwareness.LayoutAwareViewEngine.LayoutView.Render(ViewContext viewContext, TextWriter writer) in c:\src\Orchard\Mvc\ViewEngines\ThemeAwareness\LayoutAwareViewEngine.cs:line 90
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
Oct 28, 2014 at 7:02 AM
Hey - just saw this, responding for future posterity :-)

I was getting this maddening error as well, got rid of it in dev by disabling shape tracing. I'm sure that's not the issue for the original poster (production site would never have shape tracing on, right?) - but hopefully this will save somebody else some time. Cheers.