Html.AntiForgeryToken throws error “Server cannot modify cookies after HTTP headers have been sent” after fresh Visual Studio install

1

Earlier this week I installed the new Visual Studio 2019 update (16.4.2) and doing this install seemed to break my install. I would continuously get errors like this only difference being different packages mentioned in the error text. I also checked the log file that the error mentions and that didn't really seem to say anything. I followed almost all the answers in this post to which none of them caused the errors to stop, so I decided to perform an uninstall and reinstall Visual Studio.

enter image description here

After the reinstall of Visual Studio I no longer get these errors, but now I can't seem to run my application. As I am getting the error

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

I have checked this post, the first answer mentions about having multiple tabs open, this is not the case for me as I am using a different browser to debug rather than my main browser, and the accepted answer from the OP isn't very informative.

I have also looked at this post, which discusses redirection, and as far as I know there is not any redirection before hand as I am getting the error on my login page which is the first page to be loaded. Code throwing the error is exactly as such

<div class="form-horizontal">
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { }))
    {
        @Html.AntiForgeryToken()
        //do stuff
    }
</div>

And the stack trace is

System.Web.HttpException
  HResult=0x80004005
  Message=Server cannot modify cookies after HTTP headers have been sent.
  Source=System.Web.WebPages
  StackTrace:
   at System.Web.Helpers.AntiXsrf.AntiForgeryTokenStore.SaveCookieToken(HttpContextBase httpContext, AntiForgeryToken token)
   at System.Web.Helpers.AntiXsrf.AntiForgeryWorker.GetFormInputElement(HttpContextBase httpContext)
   at System.Web.Helpers.AntiForgery.GetHtml()
   at System.Web.Mvc.HtmlHelper.AntiForgeryToken()
   at ASP._Page_Views_Home_Index_cshtml.Execute() in C:\fakepath\Views\Home\Index.cshtml:line 59
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(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)

  This exception was originally thrown at this call stack:
    System.Web.HttpResponse.BeforeCookieCollectionChange()
    System.Web.HttpCookieCollection.Set(System.Web.HttpCookie)
    System.Web.Helpers.AntiXsrf.AntiForgeryTokenStore.SaveCookieToken(System.Web.HttpContextBase, System.Web.Helpers.AntiXsrf.AntiForgeryToken)
    System.Web.Helpers.AntiXsrf.AntiForgeryWorker.GetFormInputElement(System.Web.HttpContextBase)
    System.Web.Helpers.AntiForgery.GetHtml()
    System.Web.Mvc.HtmlHelper.AntiForgeryToken()
    ASP._Page_Views_Home_Index_cshtml.Execute() in Index.cshtml
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
    System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
    System.Web.WebPages.StartPage.RunPage()
    ...
    [Call Stack Truncated]

I want to add that my code was working before the uninstall and reinstall so it is quite possible I am missing something that I am not aware of, so for reference I have these Workloads installed from the VS Installer

  • ASP.NET and web development
  • Azure development
  • Python development
  • .NET desktop development
  • Data storage and processing
  • Visual Studio extension development
  • Office/SharePoint development
  • .NET Core cross-platform development
c#
asp.net
visual-studio
asked on Stack Overflow Jan 9, 2020 by WhatsThePoint

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0