liveBook · Manning

Razor expression

As explained in Pro ASP.NET Core 7, Tenth Edition

A Razor expression is an embedded expression in a Razor view that Razor evaluates to produce content in the generated response.

A Razor expression puts a C# value into the HTML generated by a Razor view. In a .cshtml file, ordinary HTML supplies the surrounding markup; Razor evaluates an expression such as @Model.Name and inserts its result into the response. A controller action can return a ViewResult and pass a view model whose properties provide those values. section 21.4.2 section 21.5

Put a product in a view

In Views/Home/Index.cshtml, <td>@Model.Name</td> inserts the product name into a table cell. <td>@Model.Price.ToString("c")</td> calls a method to format its price. A view model supplies the properties; declaring its type lets expressions in the view be type-checked. section 21.2.3 section 21.5

Use parentheses when an expression is more complex than a property access or method call. In <td>@Model.Price * 0.2m</td>, Razor treats the asterisk and number as static content: a price of 275.00 produces 275.00 * 0.2m. Write <td>@(Model.Price * 0.2m)</td> to evaluate the multiplication instead; the example produces 55.000. Check the rendered response when code and literal text meet—Razor's boundary may not be the one you intended. section 21.4.3 section 21.4.4

Expressions also work in attribute values. For example, <table data-id="@Model.ProductId"> renders with data-id="1" when that property is 1. Inspect the HTML response to verify the resulting attribute, rather than relying only on how the page looks. section 21.4.4

The @ prefix has other jobs in a view. @if and @switch select content, @foreach repeats it, and @{ ... } defines a code block. A block such as @{ Layout = null; } sets a layout choice; it is not the same as inserting a value with @<expression>. Layouts can use @RenderBody() to insert a view's content. section 21.4.2 section 2.3.3 section 7.1.6

Razor Pages use the same mixture of HTML and expressions. There, @Model.Product?.Name reads through the page model to set an element's content. Behind the scenes, Razor turns page HTML and expressions into calls such as WriteLiteral and Write in a generated C# class. section 23.2.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works through property access, method calls, arithmetic boundaries, and attributes, then identifies generated C# calls without tracing compilation in detail.
Examples
Uses a product table, currency formatting, contrasting tax calculations, a rendered data-id attribute, and page-model property access.
What it assumes
Assumes basic C# and HTML while introducing how views receive model data and produce responses.
What you can do after
Build it from scratch in a .cshtml view to insert model values and avoid mistaking arithmetic for literal content.
How it connects
Connects Razor expressions to view models, controller action results, HTML attributes, layouts, and Razor Pages.

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 Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.