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
