filmov
tv
Razor Syntax in ASP net MVC | Part - 12

Показать описание
Razor Syntax in ASP net MVC:
Requirement: Visual studio 2015 | MVC 5 version
Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write mix of HTML and server-side code using C# or Visual Basic. Razor view with visual basic syntax has .vbhtml file extension and C# syntax has .cshtml file extension.
Razor syntax has following benefits:
Compact: Razor syntax is compact which enables you to minimize number of characters and keystrokes required to write a code.
Easy to Learn: Razor syntax is easy to learn where you can use your familiar language C# or Visual Basic.
Intellisense: Razor syntax supports statement completion within Visual Studio.
Example:
Start with @ symbol to write server side C# or VB code with Html code.
For example, write @Variable_Name to display a value of a server side variable.
For example, DateTime.Now returns a current date and time.
So, write @DateTime.Now to display current datetime as shown below.
A single line expression does not require a semicolon at the end of the expression.
Razor syntax demo
@DateTime.Now.ToShortDateString()
Example: Server-side Code in Razor Syntax
@{
var date = DateTime.Now.ToShortDateString();
var message = "Hello World";
}
Today's date is: @date
@message
Requirement: Visual studio 2015 | MVC 5 version
Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write mix of HTML and server-side code using C# or Visual Basic. Razor view with visual basic syntax has .vbhtml file extension and C# syntax has .cshtml file extension.
Razor syntax has following benefits:
Compact: Razor syntax is compact which enables you to minimize number of characters and keystrokes required to write a code.
Easy to Learn: Razor syntax is easy to learn where you can use your familiar language C# or Visual Basic.
Intellisense: Razor syntax supports statement completion within Visual Studio.
Example:
Start with @ symbol to write server side C# or VB code with Html code.
For example, write @Variable_Name to display a value of a server side variable.
For example, DateTime.Now returns a current date and time.
So, write @DateTime.Now to display current datetime as shown below.
A single line expression does not require a semicolon at the end of the expression.
Razor syntax demo
@DateTime.Now.ToShortDateString()
Example: Server-side Code in Razor Syntax
@{
var date = DateTime.Now.ToShortDateString();
var message = "Hello World";
}
Today's date is: @date
@message