RazorSyntax.razor

Razor Syntax


Expressions

To switch from HTML mode to C# mode an @ is used, when the expression becomes more complex you need to add parentheses around it!

DateTime: 12/28/2025 02:10:11

Some calculation 28 *6/3 + 1

Some calculation 20

Hello World! + "!!!"

Hello World!!!!

We can call code from the code block @code{}, most of the time it's placed after the Html/Razor, scroll to the bottom of this file to take a look.


Statements can be placed in code-blocks

Joris Lops


Control flow

The Razor engine is smart, most of the time it understands when to switch from HTML mode to C# mode.

  • Joris
  • Jos
  • Nick
  • Empty name
  • Dick

Take a look at the Weather.razor code, here we need to add an @ sometimes.


No Html content (bad idea)

Some text (no html, bad idea) Some text (no html, bad idea)

Comment, are not displayed (rendered) in the browser

Tip: use the comment/uncomment shortcut to create a comment (ctrl + /) in Rider.


Display html from C# string.

Some html from C# string

Render Fragments

Render fragments are used to render a part of the page.

Hello World!

Render fragments with parameters.

Hello Joris!


An unhandled error has occurred. Reload 🗙