Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlString vs. MvcHtmlString

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.

like image 658
stacker Avatar asked Aug 01 '10 16:08

stacker


People also ask

What is MvcHtmlString?

Creates an HTML-encoded string using the specified text value. IsNullOrEmpty(MvcHtmlString) Determines whether the specified string contains content or is either null or empty.

What is HTML raw in MVC?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Configuring Bundles. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project. Using Bundles (ScriptBundle) in ASP.Net MVC Razor.


2 Answers

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.

like image 178
Rup Avatar answered Nov 26 '22 14:11

Rup


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)

like image 27
SLaks Avatar answered Nov 26 '22 16:11

SLaks