title: Magic strings in ASP.NET MVC
body: <p>I have a background in desktop software development and am getting started with learning ASP.NET MVC.</p>
  
  <p>In my default HomeController I have the Index action which has code that looks like this:</p>
  
  <pre><code>if (!Request.IsAuthenticated)
      return RedirectToAction("Login", "Account");
  </code></pre>
  
  <p>In other words, redirect the user to "/account/login". The AccountController.Login action will then handle the user and send him back to the HomeController once he logs in successfully.</p>
  
  <p>This code smells to me, perhaps just because I'm accustomed to doing things differently in desktop software. What if I change the name of the Login action to "LogOn"? What if I remove the AccountController altogether and replace it with something else? I will introduce a new bug but I won't get compiler errors, and my unit tests probably won't catch it either. Since I used strings to specify controller and action names, refactoring and redesigning has more potential to break code all over the place.</p>
  
  <p>What I would like is something like this:</p>
  
  <pre><code>if (!Request.IsAuthenticated)
      return RedirectToAction(() =&gt; AccountController.Login);
  </code></pre>
  
  <p>However I'm not sure if that's even possible or if it's the best way to do it.</p>
  
  <p>Am I being stupid, or have other people had the same problem? What do you do to get around it?</p>