I am creating a MVC-Project. Using MVC 4 and Razor. After building some pages I was wondering: what is the difference between
MvcHtmlString.Create()
and
Html.Raw()
Would be nice if you could help me here to understand that.
Thanks in advance!
This is an excellent opportunity to look at the source code that's available to us for ASP.NET (http://aspnetwebstack.codeplex.com).
Looking at HtmlHelper.cs, this is the code for
Html.Raw()
:And this is the code for the MvcHtmlString class:
The most significant difference is that
Html.Raw()
accepts any object, whileMvcHtmlString.Create()
only accepts strings. Also,Html.Raw()
returns an interface, while the Create method returns an MvcHtmlString object. Lastly, the Create deals with null differently.There is no practical difference.
The
MvcHtmlString.Create
creates an instance ofMvcHtmlString
, while theHtml.Raw
method creates an instance ofHtmlString
, butMvcHtmlString
just inherits fromHtmlString
, so they work the same.