Share the ASP. Razor syntax

Source: Internet
Author: User

ASP. NET Razor-c# and VB code syntax

Razor supports both C # (C Sharp) and VB (Visual Basic).

The main Razor C # syntax rules

Razor code block included at @ {...} In

inline expressions (variables and functions) begin with @

The code statement ends with a semicolon

Variables declared with the VAR keyword

The string is enclosed in quotation marks.

C # code is case-sensitive

C # files have the. cshtml extension.

C # instances

<!--single statement block-->@{var mymessage = "Hello World";} <!--Inline expression or variable--><p>the value of Mymessage is: @myMessage </p> <!--multi-stateme NT Block-->@{var greeting = "Welcome to our site!"; var WeekDay = Datetime.now.dayofweek;var GreetingMessage = greeting + "Here's Huston it is:" + WeekDay;} <p>the greeting is: @greetingMessage </p>

The main razor VB grammar rules

Razor code block included in @code ... In the end code

inline expressions (variables and functions) begin with @

Variables declared with the Dim keyword

The string is enclosed in quotation marks.

VB code is case insensitive

The VB file extension is. vbhtml

Instance

<!--single statement block-to @Code Dim mymessage = "Hello World" End code<!--Inline expression or variable-- > <p>the value of mymessage is: @myMessage </p> <!--multi-statement block-to-@Codedim greeting = "We Lcome to We site! "Dim WeekDay = DateTime.Now.DayOfWeek Dim GreetingMessage = Greeting &" Here's Huston it is: "&A mp Weekdayend Code <p>the Greeting is: @greetingMessage </p>

How does it work?

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

The razor syntax is a partial microsoft.net framework based on the ASP. NET Framework that is designed to create Web applications.

The razor syntax supports all the features of ASP, but uses a simplified syntax that is easier for beginners to learn and more efficient for experts.

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

When the server reads the page, it first runs the razor code and then sends the HTML page to the browser. Code executed on the server can perform some tasks that cannot be done on the browser, such as accessing the server database. The server code can create dynamic HTML content and then send it to the browser. From the browser, the HTML generated by the server code is no different from the static HTML content.

The Razor syntax of an ASP. NET page has a special file extension of cshtml (Razor C #) or vbhtml (Razor VB).

Working with objects

Server coding often involves objects.

The Date object is a typical built-in ASP. NET object, but objects can also be customized, a Web page, a text box, a file, a database record, and so on.

Object useful a database record may have a "save" method, an Image object may have a "rotation" method, an e-mail object may have a "send" method, and so on.

Objects also have properties that describe their characteristics. A database record may have FirstName and LastName properties.

Asp. Net Date object has a current attribute (written as Date.now), now the attribute has a day attribute (written as Date.Now.Day). The following example shows how to access some of the properties of a data object:

Instance

<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

An important feature of dynamic Web pages is that you can decide what to do depending on the conditions.

The usual way to do this is to use the IF ... else statement:

Instance

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

Read user input

Another important feature of dynamic Web pages is that you can read user input.

The input is read by the request [] function, and the input data is transmitted through the Ispost condition:

Instance

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

"Recommended"

1. ASP. NET free Video Tutorials

2. Share the ASP. NET Learning Notes (1)--webpages Razor

3. Share the ASP. NET Learning Notes (2)--webpages Introduction

4. Share the ASP. NET Learning Notes (3) Webpages layout

5. Share the ASP. NET Learning Notes (4) folder

6. Share the ASP. NET Learning Notes (5) global page AppStart and Pagestart

7. Share the ASP. Razor Introduction

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.