Custom tag helper not working












16















I followed a few guides on creating a custom tag helper for ASP Core.



This is my helper:



using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;

namespace ToolControlSystem.TagHelpers
{
[HtmlTargetElement("description", Attributes = DescriptionAttributeName, TagStructure = TagStructure.NormalOrSelfClosing)]
public class DescriptionTagHelper : TagHelper
{
private const string DescriptionAttributeName = "asp-for";


[HtmlAttributeName(DescriptionAttributeName)]
public ModelExpression Model { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);

var description = GetDescription(Model.ModelExplorer);

output.TagName = "span";
output.TagMode = TagMode.StartTagAndEndTag;
output.Content.SetContent(description);
}

private string GetDescription(ModelExplorer modelExplorer)
{
string description;
description = modelExplorer.Metadata.Placeholder;

if (String.IsNullOrWhiteSpace(description))
{
description = modelExplorer.Metadata.Description;
}

return description;
}
}
}


I drop this in _ViewImports.cshtml: @addTagHelper *, ToolConstrolSystem.TagHelpers



Annnndd... nothing. No intellisense, no tag replacing...



Any ideas?










share|improve this question





























    16















    I followed a few guides on creating a custom tag helper for ASP Core.



    This is my helper:



    using Microsoft.AspNetCore.Mvc.ViewFeatures;
    using Microsoft.AspNetCore.Razor.TagHelpers;
    using System;

    namespace ToolControlSystem.TagHelpers
    {
    [HtmlTargetElement("description", Attributes = DescriptionAttributeName, TagStructure = TagStructure.NormalOrSelfClosing)]
    public class DescriptionTagHelper : TagHelper
    {
    private const string DescriptionAttributeName = "asp-for";


    [HtmlAttributeName(DescriptionAttributeName)]
    public ModelExpression Model { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
    base.Process(context, output);

    var description = GetDescription(Model.ModelExplorer);

    output.TagName = "span";
    output.TagMode = TagMode.StartTagAndEndTag;
    output.Content.SetContent(description);
    }

    private string GetDescription(ModelExplorer modelExplorer)
    {
    string description;
    description = modelExplorer.Metadata.Placeholder;

    if (String.IsNullOrWhiteSpace(description))
    {
    description = modelExplorer.Metadata.Description;
    }

    return description;
    }
    }
    }


    I drop this in _ViewImports.cshtml: @addTagHelper *, ToolConstrolSystem.TagHelpers



    Annnndd... nothing. No intellisense, no tag replacing...



    Any ideas?










    share|improve this question



























      16












      16








      16


      2






      I followed a few guides on creating a custom tag helper for ASP Core.



      This is my helper:



      using Microsoft.AspNetCore.Mvc.ViewFeatures;
      using Microsoft.AspNetCore.Razor.TagHelpers;
      using System;

      namespace ToolControlSystem.TagHelpers
      {
      [HtmlTargetElement("description", Attributes = DescriptionAttributeName, TagStructure = TagStructure.NormalOrSelfClosing)]
      public class DescriptionTagHelper : TagHelper
      {
      private const string DescriptionAttributeName = "asp-for";


      [HtmlAttributeName(DescriptionAttributeName)]
      public ModelExpression Model { get; set; }

      public override void Process(TagHelperContext context, TagHelperOutput output)
      {
      base.Process(context, output);

      var description = GetDescription(Model.ModelExplorer);

      output.TagName = "span";
      output.TagMode = TagMode.StartTagAndEndTag;
      output.Content.SetContent(description);
      }

      private string GetDescription(ModelExplorer modelExplorer)
      {
      string description;
      description = modelExplorer.Metadata.Placeholder;

      if (String.IsNullOrWhiteSpace(description))
      {
      description = modelExplorer.Metadata.Description;
      }

      return description;
      }
      }
      }


      I drop this in _ViewImports.cshtml: @addTagHelper *, ToolConstrolSystem.TagHelpers



      Annnndd... nothing. No intellisense, no tag replacing...



      Any ideas?










      share|improve this question
















      I followed a few guides on creating a custom tag helper for ASP Core.



      This is my helper:



      using Microsoft.AspNetCore.Mvc.ViewFeatures;
      using Microsoft.AspNetCore.Razor.TagHelpers;
      using System;

      namespace ToolControlSystem.TagHelpers
      {
      [HtmlTargetElement("description", Attributes = DescriptionAttributeName, TagStructure = TagStructure.NormalOrSelfClosing)]
      public class DescriptionTagHelper : TagHelper
      {
      private const string DescriptionAttributeName = "asp-for";


      [HtmlAttributeName(DescriptionAttributeName)]
      public ModelExpression Model { get; set; }

      public override void Process(TagHelperContext context, TagHelperOutput output)
      {
      base.Process(context, output);

      var description = GetDescription(Model.ModelExplorer);

      output.TagName = "span";
      output.TagMode = TagMode.StartTagAndEndTag;
      output.Content.SetContent(description);
      }

      private string GetDescription(ModelExplorer modelExplorer)
      {
      string description;
      description = modelExplorer.Metadata.Placeholder;

      if (String.IsNullOrWhiteSpace(description))
      {
      description = modelExplorer.Metadata.Description;
      }

      return description;
      }
      }
      }


      I drop this in _ViewImports.cshtml: @addTagHelper *, ToolConstrolSystem.TagHelpers



      Annnndd... nothing. No intellisense, no tag replacing...



      Any ideas?







      c# asp.net-core model asp.net-core-tag-helpers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 11 '18 at 22:47









      Camilo Terevinto

      18.7k63666




      18.7k63666










      asked Jan 15 '18 at 22:23









      Matthew GoulartMatthew Goulart

      1,008927




      1,008927
























          3 Answers
          3






          active

          oldest

          votes


















          47














          You need to provide only assembly name in the view imports file.



          _ViewImports.cshtml: 

          @addTagHelper *, ToolConstrolSystem





          share|improve this answer





















          • 5





            This was really annoying. Thanks for the answear.

            – Edgar Salazar
            Apr 6 '18 at 16:52






          • 5





            Wow, that should be at least (!) in bold in documentation. Spent a few hours too guessing what did go wrong.

            – Mikhail
            Apr 21 '18 at 10:17






          • 3





            Documentation makes it look like it's a namespace, not an assembly name. *faceplam*

            – qJake
            Jun 12 '18 at 20:54











          • The assembly name!! I had the namespace. 3 hours lost until I read this.

            – Alpha75
            Nov 28 '18 at 11:54











          • TimeSpentOnThinkingItWasANamespace+=TimeSpan.FromMinutes(15);

            – agrath
            Jan 6 at 2:43



















          3














          See Managing Tag Helper scope



          @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
          @addTagHelper *, AuthoringTagHelpers


          The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.






          share|improve this answer































            0














            So I wasted a bit of time chasing the problem where a taghelper was not working. After a while I went to bed and today with fresh eyes I gave it another try, was then that I realized that I did not add the @addTagHelper in the Razor Pages folder only in the Views folder.



            So if you like I have this /Pages + /Views concepts make sure to go through all those _ViewImports.cshtml.
            Leaving this note here hopefully will remind someelse's tired brain, and if it helped take a break and go for a walk or a snooze.



            enter image description here






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48271514%2fcustom-tag-helper-not-working%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              47














              You need to provide only assembly name in the view imports file.



              _ViewImports.cshtml: 

              @addTagHelper *, ToolConstrolSystem





              share|improve this answer





















              • 5





                This was really annoying. Thanks for the answear.

                – Edgar Salazar
                Apr 6 '18 at 16:52






              • 5





                Wow, that should be at least (!) in bold in documentation. Spent a few hours too guessing what did go wrong.

                – Mikhail
                Apr 21 '18 at 10:17






              • 3





                Documentation makes it look like it's a namespace, not an assembly name. *faceplam*

                – qJake
                Jun 12 '18 at 20:54











              • The assembly name!! I had the namespace. 3 hours lost until I read this.

                – Alpha75
                Nov 28 '18 at 11:54











              • TimeSpentOnThinkingItWasANamespace+=TimeSpan.FromMinutes(15);

                – agrath
                Jan 6 at 2:43
















              47














              You need to provide only assembly name in the view imports file.



              _ViewImports.cshtml: 

              @addTagHelper *, ToolConstrolSystem





              share|improve this answer





















              • 5





                This was really annoying. Thanks for the answear.

                – Edgar Salazar
                Apr 6 '18 at 16:52






              • 5





                Wow, that should be at least (!) in bold in documentation. Spent a few hours too guessing what did go wrong.

                – Mikhail
                Apr 21 '18 at 10:17






              • 3





                Documentation makes it look like it's a namespace, not an assembly name. *faceplam*

                – qJake
                Jun 12 '18 at 20:54











              • The assembly name!! I had the namespace. 3 hours lost until I read this.

                – Alpha75
                Nov 28 '18 at 11:54











              • TimeSpentOnThinkingItWasANamespace+=TimeSpan.FromMinutes(15);

                – agrath
                Jan 6 at 2:43














              47












              47








              47







              You need to provide only assembly name in the view imports file.



              _ViewImports.cshtml: 

              @addTagHelper *, ToolConstrolSystem





              share|improve this answer















              You need to provide only assembly name in the view imports file.



              _ViewImports.cshtml: 

              @addTagHelper *, ToolConstrolSystem






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 2:30

























              answered Jan 16 '18 at 0:39









              AnurajAnuraj

              12.9k33763




              12.9k33763








              • 5





                This was really annoying. Thanks for the answear.

                – Edgar Salazar
                Apr 6 '18 at 16:52






              • 5





                Wow, that should be at least (!) in bold in documentation. Spent a few hours too guessing what did go wrong.

                – Mikhail
                Apr 21 '18 at 10:17






              • 3





                Documentation makes it look like it's a namespace, not an assembly name. *faceplam*

                – qJake
                Jun 12 '18 at 20:54











              • The assembly name!! I had the namespace. 3 hours lost until I read this.

                – Alpha75
                Nov 28 '18 at 11:54











              • TimeSpentOnThinkingItWasANamespace+=TimeSpan.FromMinutes(15);

                – agrath
                Jan 6 at 2:43














              • 5





                This was really annoying. Thanks for the answear.

                – Edgar Salazar
                Apr 6 '18 at 16:52






              • 5





                Wow, that should be at least (!) in bold in documentation. Spent a few hours too guessing what did go wrong.

                – Mikhail
                Apr 21 '18 at 10:17






              • 3





                Documentation makes it look like it's a namespace, not an assembly name. *faceplam*

                – qJake
                Jun 12 '18 at 20:54











              • The assembly name!! I had the namespace. 3 hours lost until I read this.

                – Alpha75
                Nov 28 '18 at 11:54











              • TimeSpentOnThinkingItWasANamespace+=TimeSpan.FromMinutes(15);

                – agrath
                Jan 6 at 2:43








              5




              5





              This was really annoying. Thanks for the answear.

              – Edgar Salazar
              Apr 6 '18 at 16:52





              This was really annoying. Thanks for the answear.

              – Edgar Salazar
              Apr 6 '18 at 16:52




              5




              5





              Wow, that should be at least (!) in bold in documentation. Spent a few hours too guessing what did go wrong.

              – Mikhail
              Apr 21 '18 at 10:17





              Wow, that should be at least (!) in bold in documentation. Spent a few hours too guessing what did go wrong.

              – Mikhail
              Apr 21 '18 at 10:17




              3




              3





              Documentation makes it look like it's a namespace, not an assembly name. *faceplam*

              – qJake
              Jun 12 '18 at 20:54





              Documentation makes it look like it's a namespace, not an assembly name. *faceplam*

              – qJake
              Jun 12 '18 at 20:54













              The assembly name!! I had the namespace. 3 hours lost until I read this.

              – Alpha75
              Nov 28 '18 at 11:54





              The assembly name!! I had the namespace. 3 hours lost until I read this.

              – Alpha75
              Nov 28 '18 at 11:54













              TimeSpentOnThinkingItWasANamespace+=TimeSpan.FromMinutes(15);

              – agrath
              Jan 6 at 2:43





              TimeSpentOnThinkingItWasANamespace+=TimeSpan.FromMinutes(15);

              – agrath
              Jan 6 at 2:43













              3














              See Managing Tag Helper scope



              @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
              @addTagHelper *, AuthoringTagHelpers


              The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.






              share|improve this answer




























                3














                See Managing Tag Helper scope



                @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
                @addTagHelper *, AuthoringTagHelpers


                The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.






                share|improve this answer


























                  3












                  3








                  3







                  See Managing Tag Helper scope



                  @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
                  @addTagHelper *, AuthoringTagHelpers


                  The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.






                  share|improve this answer













                  See Managing Tag Helper scope



                  @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
                  @addTagHelper *, AuthoringTagHelpers


                  The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 13 '18 at 20:08









                  RickAndMSFTRickAndMSFT

                  11k53955




                  11k53955























                      0














                      So I wasted a bit of time chasing the problem where a taghelper was not working. After a while I went to bed and today with fresh eyes I gave it another try, was then that I realized that I did not add the @addTagHelper in the Razor Pages folder only in the Views folder.



                      So if you like I have this /Pages + /Views concepts make sure to go through all those _ViewImports.cshtml.
                      Leaving this note here hopefully will remind someelse's tired brain, and if it helped take a break and go for a walk or a snooze.



                      enter image description here






                      share|improve this answer




























                        0














                        So I wasted a bit of time chasing the problem where a taghelper was not working. After a while I went to bed and today with fresh eyes I gave it another try, was then that I realized that I did not add the @addTagHelper in the Razor Pages folder only in the Views folder.



                        So if you like I have this /Pages + /Views concepts make sure to go through all those _ViewImports.cshtml.
                        Leaving this note here hopefully will remind someelse's tired brain, and if it helped take a break and go for a walk or a snooze.



                        enter image description here






                        share|improve this answer


























                          0












                          0








                          0







                          So I wasted a bit of time chasing the problem where a taghelper was not working. After a while I went to bed and today with fresh eyes I gave it another try, was then that I realized that I did not add the @addTagHelper in the Razor Pages folder only in the Views folder.



                          So if you like I have this /Pages + /Views concepts make sure to go through all those _ViewImports.cshtml.
                          Leaving this note here hopefully will remind someelse's tired brain, and if it helped take a break and go for a walk or a snooze.



                          enter image description here






                          share|improve this answer













                          So I wasted a bit of time chasing the problem where a taghelper was not working. After a while I went to bed and today with fresh eyes I gave it another try, was then that I realized that I did not add the @addTagHelper in the Razor Pages folder only in the Views folder.



                          So if you like I have this /Pages + /Views concepts make sure to go through all those _ViewImports.cshtml.
                          Leaving this note here hopefully will remind someelse's tired brain, and if it helped take a break and go for a walk or a snooze.



                          enter image description here







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 24 '18 at 15:26









                          Rui LimaRui Lima

                          4,72332237




                          4,72332237






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48271514%2fcustom-tag-helper-not-working%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              Npm cannot find a required file even through it is in the searched directory