title: Pass returnUrl from ActionLink from view to Login Form that redirects back to view
body: <p>I have read many SO posts about <strong>returnUrl</strong> as it pertains to forms authentication by default of <code>[authorize] MyController</code>, but I haven't read anything on simply passing the returnUrl where the only authentication takes place after the [HttpPost] with a login or register form  and anonymous users.
  In this case, I want the redirect to come from the original link and passed to the action where the users are authenticated via form authentication. This redirect should take the user back to the page being viewed after they 1) click register or login ActionLinks and then 2) submit the form successfully. This is on a dev server so HTTPS is not a requirement ATM.</p>
  
  <p><strong>Here is the elements without the necessary syntax/code for passing returnUrl</strong></p>
  
  <p><strong>_LoginPartial:</strong></p>
  
  <pre><code>&lt;li&gt;@Html.ActionLink("Register", "Register", "Account", routeValues: null}, htmlAttributes: new { id = "registerLink" })&lt;/li&gt; //returnUrl???
  &lt;li&gt;@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })&lt;/li&gt; // returnUrl???
  </code></pre>
  
  <p><strong>Login View:</strong></p>
  
  <pre><code>@using (Html.BeginForm()){...} //new { returnUrl } ???
  </code></pre>
  
  <p><strong>Login Get ActionResult:</strong></p>
  
  <pre><code>[AllowAnonymous]
  public ActionResult Login(string returnUrl)
  {   
      //There is other ways to store the route
      TempData["ReturnUrl"] = returnUrl;
      return View();
  }
  </code></pre>
  
  <p><strong>Login Post ActionResult:</strong></p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public ActionResult Login(LoginModel model)
  {
      if (ModelState.IsValid &amp;&amp; WebSecurity.Login(model.UserName, model.Password,     persistCookie: model.RememberMe))
      {
          return RedirectToLocal(TempData["ReturnUrl"].ToString());
      }
  
      // If we got this far, something failed, redisplay form
      ModelState.AddModelError("", "The user name or password provided is incorrect.");
      return View(model);
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Solution</strong>
  Thanks to <a href="https://stackoverflow.com/users/3444276/slightlymoist">SlightlyMoist</a> I was able to resolve the issue. While the code is small in size the ability and know-how of <code>ViewContext.RouteData.Values["key"]</code> seems to be invaluable IMO. So the only modification done as per SM was done inside the ActionLinks of the <strong>_LoginPartial</strong> view:</p>
  
  <pre><code>&lt;li&gt;
  @Html.ActionLink("Register", "Register", "Account", routeValues: new {@returnUrl = Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), ViewContext.RouteData.Values["id"])}, htmlAttributes: new { id = "registerLink" })
  &lt;/li&gt;
  
  &lt;li&gt;
  @Html.ActionLink("Log in", "Login", "Account", routeValues: new {@returnUrl = Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), ViewContext.RouteData.Values["id"])}, htmlAttributes: new { id = "loginLink" })
  &lt;/li&gt;
  </code></pre>
  
  <p><strong>ALSO</strong> 
  As per SM yet again, the <code>ViewContext.HttpContext.Request.Url.PathAndQuery</code> works as well:</p>
  
  <pre><code>&lt;li&gt;
  @Html.ActionLink("Register", "Register", "Account", routeValues: new {@returnUrl = ViewContext.HttpContext.Request.Url.PathAndQuery},htmlAttributes: new { id = "registerLink" })
  &lt;/li&gt;
  </code></pre>