ASP. NET Razor--asp.net razor-c# code syntax

Source: Internet
Author: User

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

The main Razor C # syntax rules
    • Razor code block included 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 # Instance <!--single statement block---
@{var mymessage = "Hello World";}

<!--Inline expression or variable-
<p>the value of Mymessage is: @myMessage </p>

<!--multi-statement 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»

The main Razor VB grammar rules
    • The Razor code block is included in the @Code ... End Code in
    • 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
Example <!--single statement block---
@Code Dim Mymessage = "Hello World" End Code

<!--Inline expression or variable-
<p>the value of Mymessage is: @myMessage </p>

<!--multi-statement Block--
@Code
Dim greeting = "Welcome to our site!"
Dim WeekDay = DateTime.Now.DayOfWeek
Dim GreetingMessage = Greeting & "Here in Huston it is:" & WeekDay
End Code

<p>the greeting is: @greetingMessage </p>
Running Instances»

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 the following 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.

An ASP. NET page with Razor syntax 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 the object can also be customized, a Web page, a text box, a file, a database record, and so on.

The object has a method for execution. A database record may have a "Save" method, an Image object may have a "Rotate" 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.

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

Example <table border= "1" >
<tr>
<th width= "100px" >Name</th>
&LT;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 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 > 12)
{txt = "good Evening";}
Else
{txt = "Good Morning";}
}
<body>
<p>the message is @txt </p>
</body>

Running Instances»

Read user input

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

The input is read through 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;
}
}
<body style= "Font-family:verdana, Arial;" >
<form action= "" method= "POST" >
<p><label for= "Text1" >first number:</label><br>
<input type= "text" name= "Text1"/></p>
<p><label for= "Text2" >second number:</label><br>
<input type= "text" name= "Text2"/></p>
<p><input type= "Submit" value= "ADD"/></p>
</form>
<p>@totalMessage </p>
</body>

Running Instances»

ASP. NET Razor--asp.net razor-c# code syntax

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.