First, what is Razor?
If you know what ASPX is, I'll tell you this: Razor is another view engine like aspx. When it comes to views, you know a lot. Since all are for the view engine, are all Ms things, two view engines, one after the other, there must be a difference. So let's get to know Razor now.
First, razor is used as a new view template by ASP. MVC3.
This exposes an important message for Razor: The interface design is flexible and focuses more on the design of the Web front-end page than the ASPX view engine. Because MVC is a design pattern that is based on web development, one of the main themes is decoupling pages from business logic. If ASPX is handy, there is no need to develop razor, so the razor here is a more web-friendly view engine based on MVC design.
Razor has a significant advantage in reducing code redundancy, enhancing code readability, and vs IntelliSense.
Look at the Razorde features:
· Compact, expressive and fluid : Razor Minimizes the number of characters that need to be typed in a file, giving you a refreshing coding experience. Unlike most templates, you don't break the thrill of knocking code because you need to label server-side blocks of code in HTML. The code parser is smart enough to infer from your code whether it is a server-side code. This makes its simple, expressive syntax input clean, fast, and fun.
· Get started : Razor is very easy to get started with, you just need to know a little new stuff to master it, and using your existing programming language and HTML knowledge is enough.
· not a new programming language : We try to avoid creating a new imperative language for Razor. Instead, we want programmers to use the razor only with the existing C#/VB (or other) programming language knowledge, and we just provide a very good markup syntax based on template-generated HTML in your chosen programming language.
· you can write with any text editor : Razor does not require any special tools and can be programmed efficiently using an old-fashioned text editor ("Notepad" is good).
· good smart sense input hint : Although Razor is not designed for a tool or code editor, it still has great smart hints in Visual Studio. We will upgrade Visual Studio 2010 and Visual Web Developer 2010 so that it has the full editor smart hints feature.
· Easy Unit Testing : The new View engine supports unit tests on views, does not require a controller or Web server, and can be included in any unit test project-no separate application domain (App-domain) is required.
Ii. Brief introduction of Razor grammar
1. Razor Grammar @ symbol
The @ character is an important symbol in razor, which is defined as the starting symbol for the Razor server code block. If we want to output a variable in the Web page, or the current date, we can use the following code:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > @{string productName = "Table lamp";} <span> @productName </span> <span> @DateTime. now.tostring ("yyyy-mm-hh") </span></ Span>
2.code block definition of razor syntax
You can use @{code} to define a block of code.
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >@{ int num1 =10; int num2 =5; int sum = NUM1 + num2; @sum;} </span>
In the code block, we write the code the same way as the usual server-side code. In addition, if output is required, such as the above output in the page, we can use the @sum to complete the output. In addition, @ (code) can output an expression of the results of the operation, the above code we can also write:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >@{ int num1 =10; int num2 =5; int sum = NUM1 + num2; @ (Num1 +num2); } </span>
3, Razor Syntax code mixed write
Razor supports code blending. Inserting HTML into a code block, inserting a razor statement in HTML is all Yes
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >@{ int num1 =10; int num2 =5; int sum = NUM1 + num2; String color = "Red"; <font color= "@color" > @sum </font>}</span>
4. Annotations of Razor grammar
The comment here refers to the server-side comment, in the razor code block, you can annotate using C # notation, namely//: (single-line comment) and/**/(multiline comment).
In addition, Razor provides a new server segment code comment that can comment on C # code, while commenting on HTML code, @**@, which is not restricted by blocks of code and can be anywhere in razor code
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >@* This is a comment <b> this is a comment </b> *@</span>
Three, ASPX and razor contrast
Now that we have learned aspx then we compare the advantages of sending razor. Here's an example to look at:
The first is ASPX:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><% int i = +; String str = "mzwu.com";%>i=<% =i%> str=<% =str%></span>
Convert to: Razor
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >@{ int i = +; String str = "mzwu.com";} [Email Protected],[email protected]</span>
As can be
seen, Razor replaces <% and%> with @, which is simpler when outputting a single variable value.
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><%for (int counter = 0; counter < counter++) {%> <span>number = <% =counter%>;</SPAN&G T;<%}%></span>
Convert Razor
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > @for (int counter = 0; counter < counter++) { <span>number = @counter;</span>}</span>
As you can see from the example, Razor has less code than ASPX and is smarter enough to automatically distinguish between C # code and HTML.
To this is only a simple contrast, found razor concise, of course, there are a lot of different, waiting for everyone to use.
Asp. NET another view engine--razor