ASP. NET Razor-C # and VB code syntax, asp. netrazor

Source: Internet
Author: User

ASP. NET Razor-C # and VB code syntax, asp. netrazor
C # Main Razor syntax rules

  • Razor code is encapsulated in @ {...}
  • In-row expressions (variables and functions) start @
  • The Code statement ends with a semicolon
  • String surrounded by quotation marks
  • C # code is case sensitive
  • C # the file extension is. cshtml.
C # instance
<! -- Single-line code block -- >@{ var myMessage = "Hello World" ;}<! -- Intra-row expression or variable --> <p> The value of myMessage is: @ myMessage </p> <! -- Multiline statement code block --> @ {var greeting = "Welcome to our site! "; Var weekDay = DateTime. now. dayOfWeek; var greetingMessage = greeting + "Here in Huston it is:" + weekDay;} <p> The greeting is: @ greetingMessage </p>

Running instances


Main Razor syntax rules of VB
  • The Razor Code block is enclosed in @ Code... End Code.
  • In-row expressions (variables and functions) start @
  • Declare variables with Dim keywords
  • String surrounded by quotation marks
  • VB is case insensitive
  • The extension of the VB file is. vbhtml.
Instance
<! -- Single-line Code block --> @ Code dim myMessage = "Hello World" End Code <! -- Intra-row expression or variable --> <p> The value of myMessage is: @ myMessage </p> <! -- Multiline statement code block --> @ Codedim greeting = "Welcome to our site! "Dim weekDay = DateTime. Now. DayOfWeek dim greetingMessage = greeting &" Here in Huston it is: "& weekDayEnd Code <p> The greeting is: @ greetingMessage </p>

Running instances


How to work?

Razor is a simple programming syntax used to embed server-side code into webpages.

The Razor syntax is based on the ASP. NET Framework, which is an integral part of Microsoft's. NET Framework especially designed for web application development.

Razor syntax gives you all the capabilities of ASP. NET, but it uses simplified syntax. If you are a beginner, it is easier to learn. If you are an expert, it is more conducive to improving productivity.

A Razor webpage can be described as an HTML page with two types of content: HTML content and Razor code.

After the server reads such a page, it first runs the Razor code before sending the HTML page to the browser. The Code executed on the server can complete tasks that cannot be completed in the browser, such as accessing the server database. The server code can create dynamic HTML content before the page is sent to the browser. From the browser's perspective, the HTML generated by the server code is no different from the static HTML content.

The ASP. NET webpage using Razor syntax has a special file extension cshtml (using the Razor Syntax of C #) or vbhtml (using Razor of VB ).


Dealing with objects

The server code often involves objects.

The "Date" object is a typical ASP. NET built-in object, but you can also define an object, a webpage, a text box, a file, or a database record.

Objects can have executable methods. Database records can be saved, image objects can be rotated, email objects can be sent, and so on.

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

The ASP. NET Date object has the Now attribute (written as Date. Now), and the Now attribute has the Day attribute (written as Date. Now. Day ). The following example shows how to access certain attributes of a Date 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>

Running instances


If and Else Conditions

An important feature of dynamic web pages is to determine the action to execute based on conditions.

A common method to achieve this is to use the if... else statement:

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

Running instances


Read user input

Another important feature of dynamic web pages is reading user input.

The input is read by the Request [] function and tested by 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;    }}

Running instances

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.