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();
    } 
}     

Leave a Comment