What are the Main Razor Syntax Rules?

Category: C SharpWhat are the Main Razor Syntax Rules?
Editor">Editor Staff asked 2 years ago

What are the Main Razor Syntax Rules?

1 Answers
Editor">Editor Staff answered 2 years ago
  • 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

 

  1. <!– Single statement block –>  
  2. @ {  
  3.     varmyMessage = “Hello World”;  
  4. }  
  5. <!– Inline expression or variable –>  
  6. < p > The value of myMessage is: @myMessage < /p>   
  7.     <!– Multi-statement block –>  
  8. @ {  
  9.     var greeting = “Welcome to our site!”;  
  10.     varweekDay = DateTime.Now.DayOfWeek;  
  11.     vargreetingMessage = greeting + ” Here in Huston it is: “ + weekDay;  
  12. } < p > The greeting is: @greetingMessage < /p>