HtmlString vs. MvcHtmlString

.Net 4.0asp.net Mvc-3

.Net 4.0 Problem Overview


HtmlString vs. MvcHtmlString

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

Edit:

One thing to prefer MvcHtmlString over HtmlString is the extension method IsNullOrEmpty of MvcHtmlString.

.Net 4.0 Solutions


Solution 1 - .Net 4.0

HtmlString only exists in ASP.NET 4.

MvcHtmlString was a compatibility shim added to MVC 2 to support both .NET 3.5 and .NET 4. Now that MVC 3 is .NET 4 only, it's a fairly trivial subclass of HtmlString presumably for MVC 2->3 for source compatibility.

If you're ever going to drop back to MVC 2 it might make sense to use IHtmlString or var for values returned from MVC functions. Alternatively I think you can now just switch to HtmlString throughout.

Solution 2 - .Net 4.0

HtmlString was only introduced in .Net 4.0.

In ASP.Net 3.5 MVC 2.0, you should use MvcHtmlString.
In .Net 4.0, you should use HtmlString. (which is simpler)

Solution 3 - .Net 4.0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstackerView Question on Stackoverflow
Solution 1 - .Net 4.0RupView Answer on Stackoverflow
Solution 2 - .Net 4.0SLaksView Answer on Stackoverflow
Solution 3 - .Net 4.0RobView Answer on Stackoverflow