title: ASP.NET MVC redirect after logon to the page a user came from
body: <p>Let's take as an example a standard default ASP.NET MVC project created by Visual Studio.</p>
  
  <p>Being on <code>../Home/About</code> page I click <strong>Log On</strong> link and get to the <code>../Account/LogOn</code> page. After logging in I'm being redirected to the <code>Home</code> page, not to the <code>Home/About</code> page from which I get to the <code>LogOn</code> page. <strong>Why is that?</strong></p>
  
  <p>Though, in the AccountController we have:</p>
  
  <pre><code>[HttpPost]
      public ActionResult LogOn(LogOnModel model, string returnUrl)
      {
          if (ModelState.IsValid)
          {
              if (MembershipService.ValidateUser(model.UserName, model.Password))
              {
                  FormsService.SignIn(model.UserName, model.RememberMe);
                  if (!String.IsNullOrEmpty(returnUrl))
                  {
                      return Redirect(returnUrl);
                  }
                  else
                  {
                      return RedirectToAction("Index", "Home");
                  }
              }
              else
              {
                  ModelState.AddModelError("", "The user name or password provided is incorrect.");
              }
          }
  
          // If we got this far, something failed, redisplay form
          return View(model);
      }
  </code></pre>
  
  <p>Is the problem in the fact that <code>string returnUrl</code> is empty after clicking <strong>Log On</strong> link?
  How to assign it a value when clicking the <strong>Log on</strong> link?</p>
  
  <p><strong>How to redirect a user, after logging in, to the page, from which he/she came from?</strong></p>
  
  <p>View code:</p>
  
  <pre><code>&lt;asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server"&gt;
  &lt;h2&gt;Log On&lt;/h2&gt;
  &lt;p&gt;
      Please enter your username and password. &lt;%: Html.ActionLink("Register", "Register") %&gt; if you don't have an account.
  &lt;/p&gt;
  
  &lt;% using (Html.BeginForm()) { %&gt;
      &lt;%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %&gt;
      &lt;div&gt;
          &lt;fieldset&gt;
              &lt;legend&gt;Account Information&lt;/legend&gt;
  
              &lt;div class="editor-label"&gt;
                  &lt;%: Html.LabelFor(m =&gt; m.UserName) %&gt;
              &lt;/div&gt;
              &lt;div class="editor-field"&gt;
                  &lt;%: Html.TextBoxFor(m =&gt; m.UserName) %&gt;
                  &lt;%: Html.ValidationMessageFor(m =&gt; m.UserName) %&gt;
              &lt;/div&gt;
  
              &lt;div class="editor-label"&gt;
                  &lt;%: Html.LabelFor(m =&gt; m.Password) %&gt;
              &lt;/div&gt;
              &lt;div class="editor-field"&gt;
                  &lt;%: Html.PasswordFor(m =&gt; m.Password) %&gt;
                  &lt;%: Html.ValidationMessageFor(m =&gt; m.Password) %&gt;
              &lt;/div&gt;
  
              &lt;div class="editor-label"&gt;
                  &lt;%: Html.CheckBoxFor(m =&gt; m.RememberMe) %&gt;
                  &lt;%: Html.LabelFor(m =&gt; m.RememberMe) %&gt;
              &lt;/div&gt;
  
              &lt;p&gt;
                  &lt;input type="submit" value="Log On" /&gt;
              &lt;/p&gt;
          &lt;/fieldset&gt;
      &lt;/div&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Edited (added):</strong></p>
  
  <p>It appeared that above view code is not very relevant to the problem, instead I should have looked at this (<strong>LogOnUserControl.ascx</strong>):</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %&gt;
  &lt;%
      if (Request.IsAuthenticated) {
  %&gt;
      Welcome &lt;b&gt;&lt;%: Page.User.Identity.Name %&gt;&lt;/b&gt;!
      [ &lt;%: Html.ActionLink("Log Off", "LogOff", "Account") %&gt; ]
  &lt;%
  }
  else {
  %&gt; 
      [ &lt;%: Html.ActionLink("Log On", "LogOn", "Account")%&gt; ]
  &lt;%
  }
  %&gt;
  </code></pre>