Another view engine of ASP. NET -- Razor

Source: Internet
Author: User

Another view engine of ASP. NET -- Razor

1. What is Razor?

If you know what ASPX is, I will tell you that Razor is another view engine like ASPX. When it comes to views, you will understand a lot. Since both view engines are MS, there must be a difference between the two view engines after the first and second view engines. Now let's get to know Razor.

First, Razor is used as a later view template by ASP. NET MVC3.

This exposes an important information of Razor: flexible interface design, more focused on the design of WEB front-end pages than the aspx view engine. Because MVC is a design model created based on WEB development, one of the main purposes is to decouple the page and business logic. If ASPX is easy to use, there is no need to develop Razor. Therefore, Razor is a view engine designed based on MVC that is more suitable for WEB development.

Razor has outstanding advantages in reducing code redundancy, enhancing code readability, and vs intelligent perception.

Let's take a look at the Razorde features:

·Compact, expressive, and smooth: Razor tries to reduce the number of characters to be typed in a file, giving you a quick coding experience. Unlike the syntax of most templates, you don't have to interrupt the pleasure of coding because you need to mark server-side code blocks in HTML. The Code Analyzer is smart enough to infer from your code whether it is server-side code. This makes the concise and expressive syntax input clean, fast, and interesting.

·Easy to use: Razor is very easy to use. You only need to know a few new things to get started with it. It is enough to use your existing programming language and HTML knowledge.

·Not a new Programming Language: Try to avoid creating a new imperative language for Razor. On the contrary, we hope that programmers can use Razor only by using the existing C #/VB (or other) programming language knowledge, but in the programming language you choose, provides a great HTML Tag syntax generated based on templates.

·You can use any text editor to write: Razor does not require any special tools. It can also be efficiently programmed using an antique Text Editor ("Notepad" is good ).

·Good smart sensor input prompt: Although Razor is not designed for a tool or code editor, it still has a great smart reminder feature in Visual Studio. Visual Studio 2010 and Visual Web Developer 2010 will be upgraded to provide the complete editor smart prompt function.

·Easy unit test: The New View engine allows you to perform unit tests on views without the need for controllers or Web servers, in addition, it can be included in any unit test project-no separate application Domain is required ).


II. Introduction to Razor syntax

1. @ symbol in Razor syntax


@ Is an important symbol in Razor. It is defined as the starting symbol of the Razor server code block. If you want to output a variable or the current date in the webpage, you can use the following code:

@ {String productName = "desk lamp";} @ productName @ DateTime. Now. ToString ("yyyy-MM-hh ")



2,Code block definition in Razor syntax


You can use @ {code} to define a piece of code block.

@{    int num1 =10;    int num2 =5;    int sum = num1 + num2;    @sum;}

In code blocks, we write code in the same way as the usual server-side code. In addition, if output is required, for example, the output result on the above page, we can use @ sum to complete the output. In addition, @ (code) can output the calculation result of an expression. The above code can also be written as follows:

@{        int num1 =10;        int num2 =5;        int sum = num1 + num2;        @(num1 +num2);    }

3. Code mixed write in Razor syntax

Razor supports mixed code writing. You can insert HTML into code blocks and Razor statements into HTML.

@{    int num1 =10;    int num2 =5;    int sum = num1 + num2;    string color ="Red";    @sum}

4. Comment on Razor syntax

The comment mentioned here refers to the comments on the server side. In the Razor code block, you can use the C # comments, which are // (single line comments) and/**/(multi-line comment ).

In addition, Razor provides a new server segment code comment, which can be C # code comments and HTML code comments ,@**@, this annotation method is not restricted by code blocks. It can be used anywhere in Razor code.

@ * This is a commentThis is a comment*@


Iii. Comparison between ASPX and Razor

Now that we have learned ASPX, we can compare the advantages of Razor. The following is an example:

First, ASPX:

<%    int i = 100;    string str = "mzwu.com";%>i=<% =i %>, str=<% =str %>

To: Razor

@{    int i = 100;    string str = "mzwu.com";}i=@i,str=@str

It can be seen that Razor replaces <% and %> with @, which makes it easier to output a single variable value.

<%for (int counter = 0; counter < 10; counter++){%>    number = <% =counter %>;<%}%>

Convert Razor

@for (int counter = 0; counter < 10; counter++){    number = @counter;}

From the example, we can see that Razor is less code than ASPX and more intelligent, and can automatically distinguish between C # code and html.


This is just a simple comparison. We found that Razor is concise and there are still many differences, waiting for you to use it.




Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.