ASP. NET MVC Razor syntax (1)

Source: Internet
Author: User

Razor is not a programming language. It is a server-side markup language.

What is Razor?

Razor is a markup syntax that allows you to embed server-based code (Visual Basic and C #) into a Web page.

When a Web page is written to the browser, the server-based code can create dynamic content. When the Web page loads, the server executes the server-based code within the page before returning the page to the browser. Because it is running on a server, this code can perform complex tasks, such as accessing a database.

Razor is based on ASP. NET, which is designed for the creation of Web applications. It has the ability to have traditional ASP. NET tags, but is easier to use and easier to learn.

code example:

<ul> @for (int i = 0; i < ten; i++) {<li>@i</li>}</ul>

C # 's main Razor syntax rules
    • Razor code encapsulated in @{...}
    • In-line expressions (variables and functions) begin with @
    • The code statement ends with a semicolon
    • string surrounded by quotation marks
    • C # code is case sensitive
    • C # files have the. cshtml extension.
C # Instances<!--single-line code block--@{ varMymessage ="Hello World"; }<!--in-line expressions or variables--><p>the value of Mymessage is: @myMessage </p> <!--multi-line statement code block--@{varGreeting ="Welcome to our site!";varWeekDay =DateTime.Now.DayOfWeek;varGreetingMessage = greeting +"Here in Huston it is:"+WeekDay;}<p>the Greeting is: @greetingMessage </p>
How does it work?

Razor is a simple programming syntax for embedding server-side code in Web pages.

The Razor syntax is based on the ASP. NET framework, which is a part of the Microsoft, specifically designed for Web application development.

The Razor syntax gives you all the capabilities of ASP. But with the simplified syntax, if you're a beginner, it's easier to learn, and if you're an expert, it's more productive.

Razor Web pages can be described as HTML pages with two kinds of content: HTML content and Razor code.

When the server reads this page, the Razor code is run first before the HTML page is sent to the browser. The code that executes on the server can accomplish tasks that cannot be done in the browser, such as accessing the server database. Server code can create dynamic HTML content before the page is sent to the browser. From the browser, the HTML generated by the server code is no different from the static HTML content.

An ASP. NET page with Razor syntax has a special file extension of cshtml (using the C # Razor syntax) or vbhtml (Razor using VB).

Dealing with objects

Server code often involves objects.

The "Date" object is a typical ASP. NET built-in object, but you can also define your own object, a Web page, a text box, a file, or a database record, and so on.

Objects can have methods that can be executed. Database records can provide a "save" method, the image object can have a "rotation" method, the E-mail object can provide a "send" method, and so on.

Objects can also have properties that describe their characteristics. Database records can have FirstName and LastName properties.

The ASP. NET Date object has the Now property (written as Date.now), and the now property has the day property (written as Date.Now.Day). The following example shows how to access some properties of a Date object:

<table border= "1" ><tr><th width= "100px" >name</th><td width= "100px" >Value</td> </tr><tr><td>Day</td><td> @DateTime. now.day</td></tr><tr><td >Hour</td><td> @DateTime .now.hour</td></tr><tr><td>minute</td>< Td> @DateTime. now.minute</td></tr><tr><td>second</td><td>@ Datetime.now.second</td></tr></td></table>

If and Else conditions

The important feature of dynamic Web pages is that they are based on conditions to determine the actions performed.

A common way to accomplish this is to use the IF ... else statement:

@{var txt = "", if (DateTime.Now.Hour >)  {txt = "good Evening";} else  {txt = "Good Morning";}} 

Read user input

Another important feature of dynamic Web pages is the reading of user input.

The input is read by the request[] function and tested by the IsPost condition:

@{var totalmessage = ""; if (IsPost)    {    var num1 = request["Text1"];    var num2 = request["Text2"];    var total = Num1. Asint () + num2. Asint ();    Totalmessage = "Total =" + total;    }} 

ASP. NET MVC Razor syntax (1)

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.