title: How to create a MVC5 MvcHtmlString in ASP.NET Core
body: <p>I wonder if someone can help with demonstrating how to create a IHtmlContent or HtmlString in ASP.NET Core, similar to what I previously have done in MVC5. I would normally declare a new MvcHtmlString method within a Helper class like so:</p>
  
  <p><strong>HtmlExtender.cs</strong></p>
  
  <pre><code>using System.Text;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Routing;
  
  namespace WebApplication1.Helpers
  {
      public static class HtmlExtender
      {
          public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string controller, string action, string area, string anchorTitle)
          {
              var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  
              var url = urlHelper.Action(action, controller, new { area });
  
              var anchor = new TagBuilder("a") {InnerHtml = HttpUtility.HtmlEncode(linkText)};
              anchor.MergeAttribute("href", url);
              anchor.Attributes.Add("title", anchorTitle);
  
              var listItem = new TagBuilder("li") {InnerHtml = anchor.ToString(TagRenderMode.Normal)};
  
              if (CheckForActiveItem(htmlHelper, controller, action, area))
              {
                  listItem.GenerateId("menu_active");
              }
  
              return MvcHtmlString.Create(listItem.ToString(TagRenderMode.Normal));
          }
  
          private static bool CheckForActiveItem(HtmlHelper htmlHelper, string controller, string action, string area)
          {
              if (!CheckIfTokenMatches(htmlHelper, area, "area"))
                  return false;
  
              if (!CheckIfValueMatches(htmlHelper, controller, "controller"))
                  return false;
  
              return CheckIfValueMatches(htmlHelper, action, "action");
          }
      }
  }
  </code></pre>
  
  <p>To use in a View would be <code>@Html.MenuLink("Home", "Home", "Index", "", "Home")</code> also the inclusion of <code>@using WebApplication1.Helpers</code> at the top of the View.</p>
  
  <p>I'm unsure as to the use of HtmlString or IHtmlContent to achieve what I need, but my method requires access to the HttpContextAccessor, but I am a little unsure how to do this.</p>
  
  <p>I have declared the HttpContextAccessor in the Startup.cs as I believe in ASP.NET Core 2.0 it is not declared by default as shown below but require assistance as how to use within the helper method.</p>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection serviceCollection)
  {
      serviceCollection.AddMvc();
      serviceCollection.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
  }
  </code></pre>