liveBook · Manning

Raw

As explained in ASP.NET Core in Action, 3e

Raw is a Razor HTML helper method that writes a string to the response without HTML encoding it.

Raw is a Razor HTML helper method that writes a string directly to the response without HTML encoding it. Use it only when the string is trusted and must be interpreted as markup; otherwise, Razor's normal encoding is safer. section 17.3.3 section 29.1

Rendering trusted markup

Given task = "<strong>Check oil</strong>", writing @Html.Raw(task) produces <li><strong>Check oil</strong></li>, so the task appears in bold. Writing @task instead encodes the tags, displaying them as text. section 17.3.3

Treat Raw as an exception to Razor's normal output behavior:

  • Avoid passing user input, or values derived from user input, to Html.Raw; embedded script can execute in other users' browsers. section 29.1
  • Prefer ordinary Razor expressions such as @value when content should appear as text, because Razor HTML-encodes the output. section 29.1
  • Prefer Tag Helpers for newer dynamic markup where they provide a clearer, editor-friendly alternative to HtmlHelper methods. section 17.3.3
<li><strong>Check oil</strong></li>

Shows the rendered list item produced when trusted markup is written without encoding. section 17.3.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines direct unencoded output, demonstrates rendered markup, and warns about XSS without detailing helper internals.
Examples
Demonstrates rendering <strong>Check oil</strong> as bold markup instead of displaying its HTML tags.
What it assumes
Assumes Razor view syntax and distinguishes ordinary encoded expressions from Html.Raw output.
What you can do after
Apply Html.Raw to trusted markup while avoiding it for user-controlled values that require HTML encoding.
How it connects
Connects Raw with Razor expressions, HTML encoding, XSS, HtmlHelper, and Tag Helpers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.