Server Cannot Append Header After HTTP headers have been sent Exception at @Html.AntiForgery

后端 未结 2 1327
北恋
北恋 2020-12-08 19:52

I\'m developing an asp.net mvc 5 application in which I was trying to redirect to the ReturnUrl by applying the code below :

[HttpPost]
[AllowAnonymous]
publ         


        
相关标签:
2条回答
  • 2020-12-08 20:07

    When Response.Redirect(anyUrl) the status code is set to 302, and the header will added to the response :

    HTTP 1.0 302 Object Moved 
    Location: http://anyurl.com
    

    And when ViewResult is executed and razor render the view the Html.AntiForgeryToken() will called, so the helper tries to add the header X-Frame-Options and some cookies to the response, it is the cause of the exception.

    But don't worry you can suppress the addition of X-Frame-Options header, just put this AntiForgeryConfig.SuppressXFrameOptionsHeader = true; in the Application_start.

    But I suggest you to change this:

    Response.Redirect(returnUrl);
    

    to

    return Redirect(returnUrl);
    

    Note

    Since .NET code was opened you can see how the AntiForgeryToken works, see here AntiForgeryWorker.

    0 讨论(0)
  • 2020-12-08 20:31

    I was getting same error with Response.Redirect(returnUrl). After changing to Response.Redirect(returnUrl, false) fixed the issue.

    0 讨论(0)
提交回复
热议问题