2

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
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
  • Does this answer your question? [Getting Html.AntiForgeryToken throws error "Server cannot modify cookies after HTTP headers have been sent"](https://stackoverflow.com/questions/32487816/getting-html-antiforgerytoken-throws-error-server-cannot-modify-cookies-after-h) – Mehdi Dehghani Jan 09 '20 at 11:05
  • @MehdiDehghani I discussed that question – WhatsThePoint Jan 09 '20 at 11:06
  • I think error is not related to VS, but you can create new project (hello world) and add minimal code to see if `@Html.AntiForgeryToken()` works or not. – Mehdi Dehghani Jan 09 '20 at 11:15
  • @MehdiDehghani `AntiForgeryToken()` doesn't throw an error on a new project – WhatsThePoint Jan 09 '20 at 11:30
  • So it isn't VS's fault, make sure you are using latest packages. or try adding packages (which you are using in your main project) one by one to that hello-world project and see which package broke the code. (also make sure the config section is same as your main project) – Mehdi Dehghani Jan 09 '20 at 12:27
  • @MehdiDehghani That's the thing, I am not sure it is a package causing this as it broke just after updating visual studio – WhatsThePoint Jan 09 '20 at 13:00
  • @MehdiDehghani a colleague who has 16.4.0 can run the code with no issue – WhatsThePoint Jan 09 '20 at 13:27
  • That's weird, did you search the problem in [VS's developercommunity](https://developercommunity.visualstudio.com/spaces/8/index.html)? – Mehdi Dehghani Jan 09 '20 at 13:31
  • @MehdiDehghani I could not find anything in there, and google searches only came up with forums that linked to the SO posts I mentioned – WhatsThePoint Jan 09 '20 at 13:34
  • Is there any way to access your project or testcase project which produce the error in VS v16.4.2? – Mehdi Dehghani Jan 09 '20 at 14:21
  • Without sharing the full code I don't think so, which I would not be comfortable with – WhatsThePoint Jan 09 '20 at 14:25
  • Okay, I'll try to reproduce the error and back to you if I find any solution. – Mehdi Dehghani Jan 09 '20 at 14:45
  • Actually, reproduce this issue is helpful for us to test(I notice that this may be inconvenience). At the very beginning, I consider this issue is caused by the version of packages/nugget packages, like Mehdi Dehghani said, but since you mentioned your colleague could run the same code in VS 16.4.0, maybe, it is caused by the latest version of VS, and report this issue to VS product team directly in [DC forum](https://developercommunity.visualstudio.com/spaces/8/index.html) may be helpful to verify this. (In VS|Help|Send Feedback|Report a Problem…) Besides, please recheck you code. – 大陸北方網友 Jan 13 '20 at 07:33

0 Answers0