title: How can I add a hash fragment to T4MVC route dictionary ActionResult?
body: <p>I have an extension method that returns an ActionResult (simplified for demonstration purposes):</p>
  
  <pre><code>public static ActionResult GetTestActionResult(this HtmlHelper htmlHelper, int productId)
  {
      return MVC.Products.Details(productId);
  }
  </code></pre>
  
  <p>I'm using this in an Html.ActionLink:</p>
  
  <pre><code>@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" });
  </code></pre>
  
  <p>I'm using a custom jQuery plugin for tabs, that uses these hash fragments for navigation. I want to add the tab which I want to open, by tagging the hash fragment onto the end of the URL.</p>
  
  <p>Html.ActionLink does have an <a href="http://msdn.microsoft.com/en-us/library/dd460522%28v=vs.98%29.aspx" rel="nofollow noreferrer">overload</a> for the Fragment, namely:</p>
  
  <pre><code>public static MvcHtmlString ActionLink(
      this HtmlHelper htmlHelper,
      string linkText,
      string actionName,
      string controllerName,
      string protocol,
      string hostName,
      string fragment,
      Object routeValues,
      Object htmlAttributes
  )
  </code></pre>
  
  <p>However, that is full of nasty magic strings, which T4MVC is designed to remove. Is there anyway to add the fragment to the route dictionary in my static extension method (GetTestActionResult)?</p>
  
  <p>Something like:</p>
  
  <pre><code>return MVC.Products.Details(productId).AddRouteValue(String.Empty, "#tab-similar-products");
  </code></pre>
  
  <p>I am aware that there are two similar questions and answers on SO, but they don't quite provide me with what I am looking for. I need to wrap the fragment into the ActionResult BEFORE passing it back to the view:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/5567721/including-hash-values-in-asp-net-mvc-url-routes/5568381#5568381">Including hash values in ASP.NET MVC URL routes</a></li>
  <li><a href="https://stackoverflow.com/questions/2920983/create-a-t4mvc-actionlink-with-hash-pound-sign/2922214#2922214">Create a T4MVC ActionLink with url fragment</a></li>
  </ol>
  
  <h1>UPDATE:</h1>
  
  <p>Using David Ebbo's fix below, I made the following changes. A bit hacky, but it works:</p>
  
  <p>First I altered my internal function that returns an ActionResult so that it would also add the fragment as a route value (not ideal but works):</p>
  
  <pre><code>return MVC.Products.Details(productId).AddRouteValue("tab", "#tab-similar-products");
  </code></pre>
  
  <p>Then in the view it copies that fragment value out of the route dictionary, then removes that route value for completeness.</p>
  
  <pre><code>// get my ActionResult with the tab fragment tagged on as a route value
  var actionResult = Html.GetTestActionResult(item.Key, Model.ClaimId);
  
  // get the tab fragment value
  var tabRoute = actionResult.GetRouteValueDictionary().FirstOrDefault(r =&gt; r.Key == "tab").Value ?? "none";
  
  // remove the route value, otherwise it will get tagged to the querystring
  actionResult.GetRouteValueDictionary().Remove("tab");
  
  // display
  @Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" }, fragment: tabRoute.ToString());
  </code></pre>
  
  <p>I'm sure there is a prettier way to return the fragment with the ActionResult, but for the moment this works. Thanks David.</p>