Convert IHtmlContent/TagBuilder to string in C#
If all you need to do is output the contents as a string, just add this method and pass your IHtmlContent object as a parameter to get the string output:
public static string GetString(IHtmlContent content)
{
using (var writer = new System.IO.StringWriter())
{
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
Adding to the answer above:
The new instance of the HtmlEncoder
doesn't work in ASP.NET Core RTM as the Microsoft.Extensions.WebEncoders
namespace was removed and the new HtmlEncoder
class is moved to a new namespace System.Text.Encodings.Web
, but this class is now written as an abstract and sealed class so you can't create a new instance or a derived class from it.
Pass HtmlEncoder.Default
to the method and it will work
public static string GetString(IHtmlContent content)
{
var writer = new System.IO.StringWriter();
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
ASP.NET Core actually introduced handful of careful optimizations. If you are building an HTML extension method, then the most efficient way is to avoid string:
public static IHtmlContent GetContent(this IHtmlHelper helper)
{
var content = new HtmlContentBuilder()
.AppendHtml("<ol class='content-body'><li>")
.AppendHtml(helper.ActionLink("Home", "Index", "Home"))
.AppendHtml("</li>");
if(SomeCondition())
{
content.AppendHtml(@"<div>
Note `HtmlContentBuilder.AppendHtml()` is Mutable
as well as Fluent/Chainable.
</div>");
}
return content;
}
Finally in the razor view, we don't even need - not valid based on Lukáš Kmoch comment below: @Html.Raw(Html.GetContent())
anymore (which used to be required in ASP.NET MVC 5)ASP.NET MVC 5 has type MvcHtmlString. You don't need to use Html.Raw()
just calling @Html.GetContent()
is sufficient and Razor will take care of all the escaping business.