Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert IHtmlContent/TagBuilder to string in C#

I am using ASP.NET 5. I need to convert IHtmlContent to String

IIHtmlContent is part of the ASP.NET 5 Microsoft.AspNet.Html.Abstractions namespace and is an interface that TagBuilder implements

Simplified I have the following method

public static IHtmlContent GetContent()
{
    return new HtmlString("<tag>blah</tag>");
}

When I reference it

string output = GetContent().ToString();

I get the following output for GetContent()

"Microsoft.AspNet.Mvc.Rendering.TagBuilder" 

and not

<tag>blah</tag>

which I want

I also tried using StringBuilder

StringBuilder html = new StringBuilder();
html.Append(GetContent());

but it also appends the same namespace and not the string value

I tried to cast it to TagBuilder

TagBuilder content = (TagBuilder)GetContent();

but TagBuilder doesn't have a method that converts to string

How do I convert IHtmlContent or TagBuilder to a string?

like image 518
zoaz Avatar asked Nov 12 '15 08:11

zoaz


3 Answers

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();
    } 
}     
like image 122
cygnim Avatar answered Nov 11 '22 20:11

cygnim


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();
}
like image 28
devfric Avatar answered Nov 11 '22 22:11

devfric


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 @Html.Raw(Html.GetContent()) anymore (which used to be required in ASP.NET MVC 5) - not valid based on Lukáš Kmoch comment below: 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.

like image 21
vulcan raven Avatar answered Nov 11 '22 20:11

vulcan raven