title: ASP.NET MVC - Extending Ajax.ActionLink method
body: <p>Is there anywhere I can see the method definition of the current Ajax.ActionLink method as I'd like to be able to extend it with a custom method but not sure on its current method definition</p>
  
  <p>EDIT: This is what I have so far:</p>
  
  <pre><code>public static class AjaxExtensions
      {
          public static IHtmlString MyActionLink(
          this AjaxHelper htmlHelper,
          string linkText,
          string action,
          string controller,
          object routeValues,
          AjaxOptions ajaxoptions,
          object htmlAttributes
      )
          {
              var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
              var anchor = new TagBuilder("a");
              anchor.InnerHtml = linkText;
              anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
              anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
              return MvcHtmlString.Create(anchor.ToString());
          }
      }
  </code></pre>
  
  <p>But obviously I need to add the AjaxOptions stuff which the default Ajax.ActionLink contains, but I'm not sure on what it's definition looks like.</p>
  
  <p>The reason for doing this is that I want the linkText to be an HtmlString as I want to do something like so: <code>@Ajax.MyActionLink("&lt;span class="highlight"&gt;Hello&lt;/span&gt; World",...)</code></p>
  
  <p>Second Edit:</p>
  
  <p>I get a compilation error now:</p>
  
  <pre><code>...does not contain a definition for 'MyActionLink' and the best extension method overload 'MyProject.Helpers.AjaxExtensions.MyActionLink(System.Web.Mvc.AjaxHelper, System.Web.HtmlString, string, object, System.Web.Mvc.Ajax.AjaxOptions)' has some invalid arguments
  </code></pre>
  
  <p>Here is my Extension Method:</p>
  
  <pre><code>public static IHtmlString MyActionLink(
          this AjaxHelper ajaxHelper,
          HtmlString linkText,
          string action,
          object routeValues,
          AjaxOptions ajaxoptions
      )
          {
              return MyActionLink(ajaxHelper, linkText, action, routeValues, ajaxoptions);
          }
  </code></pre>
  
  <p>Here is how I use it:</p>
  
  <pre><code>@Ajax.MyActionLink(@Html.Raw(item.ITEMID.Replace((string)ViewData["ID"], "&lt;span class='highlighted'&gt;" + (string)ViewData["ID"] + "&lt;/span&gt;")), "MoreDetails", new { id = item.ITEMID }, new AjaxOptions()
                         {
                             UpdateTargetId = "MoreDetails-" + item.ITEMID,
                             InsertionMode = InsertionMode.Replace
                         })
  </code></pre>
  
  <p>Third Edit:</p>
  
  <p>I changed my Extension method:</p>
  
  <pre><code>public static IHtmlString MyActionLink(
              this AjaxHelper ajaxHelper,
              IHtmlString linkText,
              string action,
              object routeValues,
              AjaxOptions ajaxoptions
          )
              {
                  return MyActionLink(ajaxHelper, linkText, action, routeValues, ajaxoptions);
              }
  </code></pre>
  
  <p>and now I get the following error:</p>
  
  <pre><code>Cannot evaluate expression because the current thread is in a stack overflow state. System.Collections.IDictionary
  </code></pre>