[RESOLVED]HtmlString vs. MvcHtmlString




What are the differences bettween those two, or when to prefer one over the other?

You cannot use HtmlString in MVC 2 or lower. It was introduced in ASP.NET 4. You should favour the use of HtmlString whenever possible. It is not exclusive to MVC.

under asp.net 4 you mean framework 4?Why was entered htmlstring? What did not suit mvhtmlstring?

It was introduced as a concrete implementation of the IHtmlString interface in ASP.NET 4, along with the <%: %> syntax for ensuring that strings are properly encoded for display. If the string is one that implements IHtmlString, it is considered safe for
display without the need for encoding.

MvcHtmlString belongs to System.Web.Mvc. It is not available to Web Forms unless you add a reference to System.Web.Mvc. HtmlString is in System.Web. This is automatically available to any ASP.NET app. 

See this article from Scott Guthrie: http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2

Leave a Reply