title: Server Cannot Append Header After HTTP headers have been sent Exception at @Html.AntiForgery
body: <p>I'm developing an asp.net mvc 5 application in which I was trying to redirect to the ReturnUrl by applying the code below :</p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  public ActionResult Login(UserLogin model, string returnUrl)
  {
      if (ModelState.IsValid)
      {
          string EncryptedPassword = GetMD5(model.Password);
          if (DataAccess.DAL.UserIsValid(model.Username, EncryptedPassword))
          {
              FormsAuthentication.SetAuthCookie(model.Username, true);
              if (String.IsNullOrEmpty(returnUrl))
              {
                  return RedirectToAction("Index", "Home");
              }
              else
              {
                  Response.Redirect(returnUrl);
              }
          }
          else
          {
              ModelState.AddModelError("", "Invalid Username or Password");
          }
      }
      return View();
  }
  </code></pre>
  
  <p>The above code is working fine, But the problem is that when I Post the login form, it gives me an Exception that I've never faced Before and I'm having difficulties resolving the exception that is generating in the view in Login.cshtml, At Line :</p>
  
  <pre><code>@Html.AntiForgeryToken()
  </code></pre>
  
  <p>And the Exception That it throws:</p>
  
  <blockquote>
    <p>Server cannot append header after HTTP headers have been sent.</p>
  </blockquote>
  
  <p>I've researched a lot but I'm unable to get to the conclusion. My application works fine when I remove @Html.AntiForgeryToken() line, But I don't want to do this, I want my application to remain cross-site request protected.</p>
  
  <p>Can Anyone Please Help me out, How do I get rid of this Exception? </p>