Sunday, July 25, 2021

What are the Main Razor Syntax Rules?

 Answer

  • Razor code blocks are enclosed in @{ ... }
  • Inline expressions (variables and functions) start with @
  • Code statements end with semicolon
  • Variables are declared with the var keyword
  • Strings are enclosed with quotation marks
  • C# code is case sensitive
  • C# files have the extension .cshtml

C# Example

<!-- Single statement block -->
@ {
    varmyMessage = "Hello World";
}
<!-- Inline expression or variable -->
< p > The value of myMessage is: @myMessage < /p>
    <!-- Multi-statement block -->
@ {
    var greeting = "Welcome to our site!";
    varweekDay = DateTime.Now.DayOfWeek;
    vargreetingMessage = greeting + " Here in Huston it is: " + weekDay;
} < p > The greeting is: @greetingMessage < /p>

No comments:

Post a Comment