liveBook · Manning

Razor expression

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

A Razor expression is an inline Razor construct that evaluates a C# expression and writes its result to the generated HTML output.

A Razor expression evaluates a C# expression and writes its result into generated HTML. Use @ for a single expression and @(...) when whitespace makes the expression boundary unclear. section 17.3.1

Writing values into HTML

Use <p>Copyright @DateTime.Now.Year</p> for a simple expression, or <p>The sum of 1 and 2 is @(1 + 2)</p> when the C# expression contains spaces. Razor expressions do not require semicolons. To display a literal @, write @@. section 17.3.1

Razor expressions commonly write PageModel or ViewData values to a view. For dynamic sections, combine expressions with C# if, for, or foreach constructs prefixed by @; use @{} instead when code should execute without producing HTML. section 17.3.1 section 17.3.2

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

How deep it goes
Defines @, @(), and @@ syntax, contrasts expressions with @{} code blocks, and extends into loops and conditionals.
Examples
Uses copyright text, arithmetic output, and a to-do list rendered conditionally from model data.
What it assumes
Assumes basic C# and HTML familiarity while teaching Razor’s transitions between markup, expressions, and code.
What you can do after
Apply it via Razor syntax to render model values and generate data-dependent HTML without manually building response strings.
How it connects
Connects C#, HTML, Razor Pages, PageModel, ViewData, code blocks, loops, conditionals, and Tag Helpers.

Also explained in

Compare how 2 books explain razor expression →

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.