18

I use the Html.Raw to print a raw html content, for example when I send some thing like ViewBag.div = "<div> Hello </div>"; from the controller to the view side it does not print a raw html content unless I use the Html.Raw method but if I have an encoded content, like content encoded using jquery and inserted into the database and I want to print it as a raw html content the Html.Raw method does not work and I have to use HttpUtility.HtmlDecode(EncodedContent) before I use Html.Raw so please could anyone explain why it acts in this way and what is the proper situation to use Html.Raw method? or in another way, why Html.Raw does not work when it receives html entities as a parameter instead of html tags?.

Mo Haidar
  • 3,748
  • 6
  • 37
  • 76

2 Answers2

34

Because encoded characters are HTML, and the Raw version of that string is the encoded one.


Html.Raw renders what it is given without doing any html encoding, so with ViewBag.div = "<div> Hello </div>";:

@Html.Raw(ViewBag.div);

Renders

<div> Hello </div>

However, when you have encoded characters in there, such as ViewBag.Something = "&gt;"; the raw version of that is &gt;. To get back to actual html you need to Html.Raw(HttpUtility.HtmlDecode(EncodedContent)); as you've said.

If Html.Raw did do the decoding then it would be confusing, and we would need something that didn't do it. ;-)

NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
5

Html.Raw Method asks the Razor Engine to not encode the special chars.

The Razor Engine encodes the special chars because it considers that you want to show them in the state you sent to it. As a result, it encodes the special chars, and the browser decodes them again to show you the characters as you sent them to the Razor Engine.

But if you use the Html.Raw you are telling the Razor Engine to not encode the special chars of your content and render the content you get as-is from, for example, your database. So, if you want to show the decoded content, you have to decode it using HttpUtility.HtmlDecode and then directly render the html tags by using Html.Raw.

Example

If you have this content in your database

&lt;h1&gt;dklxf;&lt;span style="font-style: italic;"&gt;kldk;dlk&lt;span style="font-weight: bold;"&gt;dxl'f;dlxd'fdlf;ldk;dlkf&lt;/span&gt;&lt;/span&gt;&lt;/h1&gt;

...if render it without using HTML.Raw the Razor Engine will encode the special chars in that content to be printed in the browser as-is, but if you use HTML.Raw it renders it to the HTML content of the page source directly. (Run the snippet)

&lt;h1&gt;dklxf;&lt;span style="font-style: italic;"&gt;kldk;dlk&lt;span style="font-weight: bold;"&gt;dxl'f;dlxd'fdlf;ldk;dlkf&lt;/span&gt;&lt;/span&gt;&lt;/h1&gt;

But if you use Html.Raw(HttpUtility.HtmlDecode(EncodedContent)) then your page will render the content as HTML tags. (Run the snippet)

<h1>dklxf;<span style="font-style: italic;">kldk;dlk<span style="font-weight: bold;">dxl'f;dlxd'fdlf;ldk;dlkf</span></span></h1>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Mo Haidar
  • 3,748
  • 6
  • 37
  • 76