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> Â Â Â Â } Â Â Â ...