title: Better way to get active page link in MVC 3 Razor
body: <p>When I want a specific menu link to be active at a given page, I'm using this approach in Razor:</p>
  
  <p>On the master layout I have these checks:</p>
  
  <pre><code>var active = ViewBag.Active;
  const string ACTIVE_CLASS = "current";
  
  if (active == "home")
  {
      ViewBag.ActiveHome = ACTIVE_CLASS;
  }
  if (active == "products")
  {
      ViewBag.ActiveProducts = ACTIVE_CLASS;
  }
  </code></pre>
  
  <p>etc.</p>
  
  <p>The html menu on the master layout:</p>
  
  <pre><code>&lt;ul&gt;
  &lt;li class="@ViewBag.ActiveHome"&gt;&lt;a href="/"&gt;Home&lt;/a&gt;&lt;/li&gt;
  &lt;li class="@ViewBag.ActiveProducts"&gt;&lt;a href="@Url.Action("index", "products")"&gt;Products&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>When specifying which layout page to use on a different view:</p>
  
  <pre><code>@{
      ViewBag.Active = "home";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  </code></pre>
  
  <p>Is there a better approach to sepcify active links, than the one I'm currently using?</p>