title: ASP.NET MVC - Back Links Wrong URL
body: <p>For some reason when going back a link, i.e. hitting the back button in your browser takes me to the wrong link in my application.  I've got a path of:</p>
  
  <pre><code>http://localhost:3340/Accounts/ContractsControl/GeneralContracts
  </code></pre>
  
  <p>and on that page I click on a link which takes me to</p>
  
  <pre><code>http://localhost:3340/Accounts/ContractsControl/GeneralContractView/223
  </code></pre>
  
  <p>now when I hit the back link it takes me to</p>
  
  <pre><code>http://localhost:3340/Accounts/ContractsControl/DataContracts
  </code></pre>
  
  <p>which is the page I went to the <code>GeneralContracts</code> page from.  I'm not entirely sure why this happens, but it means users hitting the back link (which is very common) will get taken to the wrong page.</p>
  
  <p><strong>Global.asax.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Routing;
  using Intranet.AreasLib;
  
  namespace Intranet
  {
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
  
    public class MvcApplication : System.Web.HttpApplication
    {
      public static void RegisterRoutes(RouteCollection routes)
      {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
        routes.MapAreas(
                "{controller}/{action}/{id}",
                "Intranet",
                new[] { "Accounts", "Client" }
          );
  
        routes.MapRootArea(
            "{controller}/{action}/{id}",
            "Intranet",
            new { controller = "Home", action = "Index", id = "" }
          );
  
      }
  
      protected void Application_Start()
      {
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new AreaViewEngine());
  
        RegisterRoutes(RouteTable.Routes);
      }
    }
  }
  </code></pre>
  
  <p><a href="http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx" rel="nofollow noreferrer">Areas by Phil Haack.</a></p>
  
  <p><strong>FIXED</strong></p>
  
  <p>The JavaScript I did have was something I wrote ages ago and just shoved in there.  Being able to click on table rows and redirect to another page which was like follows:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function() {
        $('tr:not(:has(th))').click(function() {
          window.location.replace("GeneralContractView/" + $(this).attr('id'));
        });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Whilst this works, it <em>should</em> be like this:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function() {
        $('tr:not(:has(th))').click(function() {
          window.location = "GeneralContractView/" + $(this).attr('id');
        });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Because <code>replace</code> doesn't do the same as assigning a new location.  It means that the URL referrer is null.  Lesson learned.</p>