Razor is an ASP.NET programming syntax used to create dynamic web pages with the C# or Visual Basic .NET programming languages. Razor was in development in June 2010[1] and was released for Microsoft Visual Studio 2010 in January 2011.[2] Razor is a simple-syntax view engine and was released as part of ASP.NET MVC 3 and the Microsoft WebMatrix tool set.[2]
The Razor syntax is a template markup syntax, based on the C# programming language, that enables the programmer to use an HTML construction workflow[clarification needed]. Instead of using the ASP.NET .ASPX markup syntax with <%= %> symbols to indicate code blocks, Razor syntax starts code blocks with a @ character and does not require explicit closing of the code-block.
Advantages of Razor[edit source | editbeta]
The idea behind Razor is to provide an optimized syntax for HTML generation using a code-focused templating approach, with minimal transition between HTML and code.[3] The design reduces the number of characters and keystrokes, and enables a more fluid coding workflow by not requiring explicitly denoted server blocks within the HTML code.[1] Other advantages that have been noted:[4]
Is not a new language (no major changes to learn)
Supports IntelliSense (statement completion support)
Unit Testable
Supports "layouts" (an alternative to the "master page" concept in classic aspx pages)
++++++++++++++++++
Main Razor Syntax Rules for C#
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 -->
@{ var myMessage = "Hello World"; }
<!-- Inline expression or variable -->
<p>The value of myMessage is: @myMessage</p>
<!-- Multi-statement block -->
@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Today is: " + weekDay;
}
<p>The greeting is: @greetingMessage</p>
+++++++++++++++++++++++++++++++++
Main Razor Syntax Rules for VB
Razor code blocks are enclosed in @Code ... End Code
Inline expressions (variables and functions) start with @
Variables are declared with the Dim keyword
Strings are enclosed with quotation marks
VB code is not case sensitive
VB files have the extension .vbhtml
Example
<!-- Single statement block -->
@Code dim myMessage = "Hello World" End Code
<!-- Inline expression or variable -->
<p>The value of myMessage is: @myMessage</p>
<!-- Multi-statement block -->
@Code
dim greeting = "Welcome to our site!"
dim weekDay = DateTime.Now.DayOfWeek
dim greetingMessage = greeting & " Today is: " & weekDay
End Code
<p>The greeting is: @greetingMessage</p>
沒有留言:
張貼留言