0

I have tried this two different ways.. not a great deal on this in the forum either.

I am trying to create a custom taghelper slowly building it up as I move forward. Its to produce a dynamic menu however I am not getting the right output from string builder.

I am trying to produce this for the precontent:

        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">

and this for the postcontent:

            </ul>
        </div>

I intended to add this as output.precontent and output.postcontent. I also decided to use a tagbuilder approach for each tag and to add them to a stringbuilder finally rendering it as the forementioned precontent. This is what I came up with.

        StringBuilder PreContentSb = new StringBuilder();

        StringBuilder PostContentSb = new StringBuilder();

        TagBuilder divTag = new TagBuilder("div");
        TagBuilder ul = new TagBuilder("ul");

        divTag.MergeAttribute("class", "navbar - collapse collapse");
        divTag.TagRenderMode = TagRenderMode.SelfClosing;

        ul.MergeAttribute("class", "nav navbar - nav");
        ul.TagRenderMode = TagRenderMode.SelfClosing;

        PreContentSb.Append(' ', 12);
        PreContentSb.Append(divTag.InnerHtml.ToString());
        PreContentSb.Append(Environment.NewLine);
        PreContentSb.Append(' ', 16);
        PreContentSb.Append(ul.ToString());
        PreContentSb.Append(Environment.NewLine);

        output.PreContent.SetContent(PreContentSb.ToString());

        PostContentSb.Append(' ', 16);
        PostContentSb.Append("</ul>");
        PreContentSb.Append(Environment.NewLine);
        PostContentSb.Append(' ', 12);
        PostContentSb.AppendLine("</div>");
        PreContentSb.Append(Environment.NewLine);

        output.PostContent.SetContent(PostContentSb.ToString());

The taghelper is only going in one place so I thought I would indent for that place to start with eg "PreContentSb.Append(' ', 12);"..

The problem I am getting is that when I look at page source I am getting, for these tags the following:

          Microsoft.Extensions.Internal.BufferedHtmlContent&#xD;&#xA;                Microsoft.AspNet.Mvc.Rendering.TagBuilder&#xD;&#xA;                &lt;/ul&gt;            &lt;/div&gt;&#xD;&#xA;

First of all its not rendering the tagbuilt tags at all and secondly instead of inverted commas and angle brackets its displaying the actual Character entity references..

How do I simply display the inverted commas, angle brackets?

Finally how do you render a tagbuilder element in a stringbuilder?

1 Answer 1

1
  1. TagBuilder doesn't have an override to ToString(). You have to use WriteTo() to write the content of it (in i.e. a TextWriter).

  2. To prevent escaping, you have to return a HtmlString. Check my answer here on how to return a sting that's not being encoded by the template engine. The example is about attributes but it should give you an idea on how to do it, i.e. output.PostContent.SetContent(new HtmlString(PostContentSb.ToString()));

Sign up to request clarification or add additional context in comments.

2 Comments

Quick question - how do I use WriteTo() to add the contents of the TagBuilder to the stringbuilder? (will up vote this)
var writer = new System.IO.StringWriter(); divTag.WriteTo(writer, new HtmlEncoder()); var s = writer.ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.