IHtmlContent ToString() not working
article code in asp.net 4:
HtmlString html1;
HtmlString html2;
html1 = helper.LabelFor(prop, htmlAttributes);
html2 = helper.LabelFor(prop, htmlAttributes);
return new HtmlString(html1.ToString() + ": " +
html2.ToString();
When I try it in ASP.net 5:
HtmlString html1 = new HtmlString(htmlHelper.LabelFor(expression).ToString());
when debugging I noticed
htmlHelper.LabelFor(expression).ToString()
value is
" Microsoft.AspNet.Mvc.Rendering.TagBuilder"
and not the html value that was expected
Calling HtmlString.ToString() was in fact never correct. It just happened to return the same string. Instead the correct code in ASP.NET 4 is to call [HtmlString.ToHtmlString()](https://msdn.microsoft.com/en-us/library/system.web.htmlstring.tohtmlstring(v=vs.110).aspx).
In ASP.NET 5 there is no IHtmlContent.ToString() operation because concatenating strings is hugely inefficient. You can instead write the IHtmlContent to a TextWriter. This avoids reallocating memory and inefficient code.
To concatenate two IHtmlContent objects you can implement an IHtmlContent that in its WriteTo method calls each "child" IHtmlContent in a row and writes them each out to the TextWriter.
@rynowak @dougbu @Tratcher @pranavkm not sure if maybe one of you remember, but why didn't we make BufferedHtmlContent public? It seems like it would be useful for scenarios like this, where someone wants to recompose other HTML bits and return them. Was there some layering issue?
We could make it public if we want to. It's really kinda a stopgap until we specialize various implementations more. I don't want to make any public API changes that make it harder to make perf changes until we're done :laughing:
Can you confirm what the fix is for this? How do we get the output of the htmlhelper?
@Alchemy86, here's what you'd have to do:
using (var writer = new StringWriter())
{
content.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
return writer.ToString();
}
Is there anything wrong with this to simplify usage?
public static string ToHtmlString(this IHtmlContent tag)
{
using (var writer = new StringWriter())
{
tag.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
return writer.ToString();
}
}
Is there anything wrong with this to simplify usage?
@agileramblings are you suggesting the extension method be in the MVC or Html.Abstractions public API? I recommend we not do this because it's rather inefficient and discourages better patterns. The old HtmlString.ToHtmlString() didn't have these issues because HtmlString contains only one string.
That said, the code is fine for a DebuggerToString() or similar non-production method.
How do we get the output of the htmlhelper?
@Alchemy86 if you need to add the tag to something else, write, copy, or move it there (depending on whether the you have target TextWriter or IHtmlContent and whether the IHtmlContent implements IHtmlContentContainer). If you need to inspect the tag, subclass TextWriter to inspect the strings it's passed and use Razor's NullHtmlEncoder. And, if you need to modify the tag, downcast to whatever types are of interest and modify those IHtmlContent implementations before they're written out.
I'd like to hear more if your requirements fall into the last bucket. We might have a need for more extensibility somewhere -- allowing you to get the IHtmlContent you want up front.
Oh, and for @giddev's use case, write html1 and html2 into a HtmlContentBuilder and return that.
@dougbu Thanks! I don't think it should be added to the public API. I had a TagBuilder instance I needed to see the HTML in and found this thread. An extension method when needed seemed like an easy solution.
@dougbu I'm confused as to what you mean by:
write, copy, or move it there (depending on whether the you have target TextWriter or IHtmlContent and whether the IHtmlContent implements IHtmlContentContainer
We have many helpers which do things like create composite HTML content via many tag builders and historically have used the implicit .ToString() to allow this. E.g.
...
var cbx = html.CheckBoxFor(expression);
var lbl = html.LabelFor(expression, metadata.Description);
var div = new TagBuilder("div");
div.AddCssClass("multiple-choice");
div.InnerHtml = $"{cbx}{lbl}";
...
Is there a better / more efficient way we should be combining tag builders?
The TagBuilder.InnerHtml property is of type IHtmlContentBuilder and the two IHtmlHelper methods return IHtmlContent. So, change the last line to
div.InnerHtml.AppendHtml(cbx);
div.InnerHtml.AppendHtml(lbl);