Building HtmlStrings in ASP.NET MVC

Judo picture Judo · Oct 26, 2011 · Viewed 13.2k times · Source

I have an extension method that needs to return an HtmlString. The method has a loop which will build the HtmlString, however the HtmlString object has no Append method and does not allow concatenation using the + operator so I am not sure how I would build the HtmlString.

I'd like to use the StringBuilder but it doesn't have a ToHtmlString method...

Any solutions or patterns for this?

Answer

Tetaxa picture Tetaxa · Oct 26, 2011

Why not just build the string in a stringbuilder and then return MvcHtmlString.Create(sb.ToString());