title: Instantiating and Invoking TagHelpers Manually
body: <p>This is a follow up question to <a href="https://stackoverflow.com/questions/42010429/can-i-use-a-tag-helper-in-a-custom-tag-helper-that-returns-html">this question</a>, which seems to be for an older ASP.NET Core version (I'm using 2.1). </p>
  
  <p>I'm trying to call a TagHelper manually from within a TagHelper. Applying the Answer in the linked question above, the TagHelper.Process looks like so:</p>
  
  <pre><code>public override async void Process(TagHelperContext context, TagHelperOutput output)
  {
      var anchorTagHelper = new AnchorTagHelper
      {
          Action = "Home",
      };
      var anchorOutput = new TagHelperOutput("a", new TagHelperAttributeList(), (useCachedResult, encoder) =&gt; new HtmlString());
      var anchorContext = new TagHelperContext(
          new TagHelperAttributeList(new[] { new TagHelperAttribute("asp-action", new HtmlString("Home")) }),
          new Dictionary&lt;object, object&gt;(),
          Guid.NewGuid());
      await anchorTagHelper.ProcessAsync(anchorContext, anchorOutput);
      output.Content.SetHtmlContent(anchorOutput);
  }
  </code></pre>
  
  <p>Several compiler errors occur at this point. </p>
  
  <blockquote>
    <p>cannot convert from 'System.Guid' to 'string'</p>
  </blockquote>
  
  <p>No problem, I can cast to a String. </p>
  
  <blockquote>
    <p>There is no argument given that corresponds to the required formal
    parameter 'value' of 'HtmlString.HtmlString(string)</p>
  </blockquote>
  
  <p>Checking the MSDN page for the <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.razor.taghelpers.taghelperoutput.-ctor?view=aspnetcore-2.2#Microsoft_AspNetCore_Razor_TagHelpers_TagHelperOutput__ctor_System_String_Microsoft_AspNetCore_Razor_TagHelpers_TagHelperAttributeList_System_Func_System_Boolean_System_Text_Encodings_Web_HtmlEncoder_System_Threading_Tasks_Task_Microsoft_AspNetCore_Razor_TagHelpers_TagHelperContent___" rel="nofollow noreferrer">TagHelperOutput constructor</a>, it seems it doesn't take an <code>HtmlString</code> anymore. </p>
  
  <p>I changed that argument to a delegate fuction:</p>
  
  <pre><code>new TagHelperOutput("a", new TagHelperAttributeList(),
      (useCachedResult, encoder) =&gt; Task.Factory.StartNew&lt;TagHelperContent&gt;(
           () =&gt; new DefaultTagHelperContent()));
  </code></pre>
  
  <p>The last compiler error:</p>
  
  <blockquote>
    <p>There is no argument given that corresponds to the required formal
    parameter 'generator' of
    'AnchorTagHelper.AnchorTagHelper(IHtmlGenerator)</p>
  </blockquote>
  
  <p>Hoping its an optional parameter, I passed in null:</p>
  
  <pre><code>var anchorTagHelper = new AnchorTagHelper(null);
  </code></pre>
  
  <hr>
  
  <p>It finally compiled - but this led to a null pointer exception on runtime:</p>
  
  <blockquote>
    <p>Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.Process(TagHelperContext
    context, TagHelperOutput output)</p>
  </blockquote>
  
  <p>My question is thus: how can I instantiate <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.taghelpers.anchortaghelper.-ctor?view=aspnetcore-2.2#Microsoft_AspNetCore_Mvc_TagHelpers_AnchorTagHelper__ctor_Microsoft_AspNetCore_Mvc_ViewFeatures_IHtmlGenerator_" rel="nofollow noreferrer"><code>AnchorTagHelper</code></a> so I can manually invoke TagHelpers in C#? </p>
  
  <p><strong>EDIT</strong>: I've DI'd IHtmlGenerator and got a more meaningful error message: </p>
  
  <pre><code>private IHtmlGenerator htmlGenerator;
  public myAnchorTagHelper(IHtmlGenerator htmlGenerator) {
      this.htmlGenerator = htmlGenerator;
  }
  </code></pre>
  
  <blockquote>
    <p>Value cannot be null. Parameter name: viewContext></p>
    
    <p>Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateActionLink(ViewContext
    viewContext, String linkText, String actionName, String
    controllerName, String protocol, String hostname, String fragment,
    Object routeValues, Object htmlAttributes)    at
    Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Process(TagHelperContext
    context, TagHelperOutput output)    at
    EAGLEweb2020.Models.EAGLEinputTagHelper.Process(TagHelperContext
    context, TagHelperOutput output) in
    C:\Users\1135937\source\repos\EAGLEweb2020\EAGLEweb2020\Models\TagHelpers\EAGLEinputTagHelper.cs:line
    66    at
    Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext
    context, TagHelperOutput output)    at
    Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.d__0.MoveNext()</p>
  </blockquote>
  
  <p>But how can I DI a ViewContext? </p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Turns out ViewContext is a public property in AnchorTagHelper:</p>
  
  <pre><code>AnchorTagHelper inputTagHelper = new AnchorTagHelper(htmlGenerator);
  inputTagHelper.ViewContext = viewContext;
  </code></pre>
  
  <p>But now the content is empty... </p>