ASP.NET Core by Matthew

Razor Markup

Razor pages use the @ symbol to transition from HTML to C#. C# expressions are evaluated and then rendered in the HTML output. You can use Razor syntax under the following conditions:

  • Anything immediately following the @ is assumed to be C# code.
  • Code blocks must appear within @{ ... } brackets.
  • A single line of code that uses spaces should be surrounded by parentheses ( ).
@page
@model PersonModel
 
// Using the `@` symbol:
<h1>My name is @Model.FirstName and I am @Model.Age years old </h1>
 
// Using a code block:
@{
 var greet = "Hey threre!";
 var name = "John";
 <h1>@greet I'm @name!</h1>
}
 
// Using parentheses:
<p>Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))</p>

Comments

Leave a Comment

All fields are required. Your email address will not be published.