liveBook · Manning

Razor compiler

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

A compiler that transforms Razor files containing HTML markup and C# code into generated classes for execution by ASP.NET Core.

The Razor compiler processes .cshtml views that mix HTML with C# expressions. It separates static HTML from expressions and handles them separately in a generated class file. The resulting HTML can become the response to an HTTP request. section 21.4 section 21.0

In an MVC request, routing selects a controller action. The action returns a ViewResult; the Razor view engine locates the view and processes it with any view model supplied by the action. With no view name specified, the engine uses the action name. An explicit name such as `View("Watersports", prod)` selects another view; the engine looks in `Views/[controller]` and then `Views/Shared`. section 2.3.4 section 21.2.4

Mark the boundary between code and HTML

In `Views/Home/Watersports.cshtml`, `@Model.Name` inserts a property value and `@Model.Price.ToString("c")` inserts a formatted value. For arithmetic, write `@(Model.Price * 0.2m)`, not `@Model.Price * 0.2m`. Without parentheses, the compiler conservatively treats the asterisk and number as static content. If a calculated value appears partly as literal text, check that boundary first. section 21.4.3

View setup also affects the HTML produced. `_ViewImports.cshtml` can make model namespaces available and enable tag helpers; `_ViewStart.cshtml` can select a shared layout whose `@RenderBody()` receives each view’s content. These files configure views, rather than changing how the compiler distinguishes a complex expression from markup. section 7.1.6 section 21.4.3

Listing 21.22 Expressions in the Watersports.cshtml file in the Views/Home folder
@model Product
<!DOCTYPE html>
<html>
<head>
    <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <h6 class="bg-secondary text-white text-center m-2 p-2">
        Watersports
    </h6>
    <div class="m-2">
        <table class="table table-sm table-striped table-bordered">
            <tbody>
                <tr><th>Name</th><td>@Model.Name</td></tr>
                <tr>
                    <th>Price</th>
                    <td>@Model.Price.ToString("c")</td>
                </tr>
                <tr><th>Tax</th><td>@Model.Price * 0.2m</td></tr>
                <tr><th>Tax</th><td>@(Model.Price * 0.2m)</td></tr>
            </tbody>
        </table>
    </div>
</body>
</html>

Compare the two Tax cells: the parenthesized expression makes the intended calculation unambiguous. section 21.4.3

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

How deep it goes
Explains separation of static HTML and C# expressions into a generated class, but does not show generated source or compilation phases.
Examples
Compares unparenthesized and parenthesized tax expressions in a product view to expose a code-versus-markup parsing mistake.
What it assumes
Builds from controller actions, HTML, and C# expressions without requiring knowledge of generated class internals.
What you can do after
Diagnose/debug with it by parenthesizing complex expressions when Razor treats part of a calculation as static content.
How it connects
Connects Razor syntax to MVC controllers, view models, shared views, layouts, and tag helpers.

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.