Learning Razor syntax
One of the things I like about the Razor syntax is that it is easy to mix code and HTML tags. This section will be a lot of theory to help us get to know the Razor syntax.
To transition from HTML to code (C#), we use the @
symbol. There are a couple of ways we can add code to our file:
- Razor code blocks
- Implicit Razor expressions
- Explicit Razor expressions
- Expression encoding
- Directives
Razor code blocks
We have already seen some code blocks. A code
block looks like this:
@code { //your code here }
If we wish, we can skip the code
keyword, like so:
@{ //your code here }
Inside those curly braces, we can mix HTML and code like this:
@{ void RenderName(string name) { <p>Name: <strong>@name</strong></p> } ...