Home >Backend Development >C#.Net Tutorial >Share ASP.NET study notes (13) Detailed explanation of Razor syntax
ASP.NET Razor - C# and VB code syntax
Razor supports both C# (C sharp) and VB (Visual Basic).
Main Razor C# syntax rules
Razor code blocks are enclosed in @{...}
Inline expressions (variables and functions) begin with @
Code statements end with semicolons
Variables are declared using the var keyword
Strings are enclosed in quotes
C#Code is case sensitive
The extension of C# files is .cshtml
C# examples
<!-- 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>
Main Razor VB syntax rules
Razor code blocks are contained in @Code…
Inline expressions (variables and functions) in the end code begin with @
Variables are declared using the Dim keyword
Strings are enclosed in quotes
VB code is not case sensitive
The extension of VB files 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 --> @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>
How does it work?
Razor is a simple programming syntax for embedding server code in web pages.
Razor syntax is based on the ASP.NET Framework, a part of the Microsoft.NET framework specifically designed for creating web applications.
Razor syntax supports all ASP.NET features, but uses a simplified syntax that is easier to learn for beginners and more efficient for experts.
A Razor web page can be described as an HTML web page with two types of content: HTML content and Razor code.
When the server reads the page, it first runs the Razor code before sending the HTML page to the browser. Code executed on the server can perform tasks that cannot be completed on the browser, such as accessing the server database. Server code can create dynamic HTML content and send it to the browser. From a browser perspective, the HTML generated by the server code is no different from the static HTML content.
Razor syntax ASP.NET web pages have special file extensions cshtml (Razor C#) or vbhtml (Razor VB).
Using 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, etc.
Object Useful A database record might have a "save" method, an image object might have a "rotate" method, an email object might have a "send" method, and so on.
Objects also have attributes used to describe their respective characteristics. A database record may have FirstName and LastName properties.
ASP.NET date objects have a now property (written as Date.Now), and the now property has a day property (written as Date.Now.Day). The following example demonstrates how to access some properties of the data object:
Example
<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 ElseCondition
An important part of dynamic web pages The thing is, you can decide what to do based on the conditions.
A common way to do this is to use an if ... else statement:
Example
@{var txt = "";if(DateTime.Now.Hour > 12){txt = "Good Evening";}else{txt = "Good Morning";}}<html><body><p>The message is @txt</p></body></html>
读取用户输入
动态网页的另一个重要特点是,您可以读取用户输入。
输入是通过请求[]功能读取的,并且传送输入数据是经过IsPost条件判断的:
实例
@{var totalMessage = "";if(IsPost){var num1 = Request["text1"];var num2 = Request["text2"];var total = num1.AsInt() + num2.AsInt();totalMessage = "Total = " + total;}}<html><body style="background-color: beige; 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></html>
【相关推荐】
2. 分享ASP.NET学习笔记(1)--WebPages Razor
3. 分享ASP.NET学习笔记(2)--WebPages 介绍
4. 分享ASP.NET学习笔记(3)WebPages 布局
6. 分享ASP.NET学习笔记(5)全局页面 AppStart 和 PageStart
The above is the detailed content of Share ASP.NET study notes (13) Detailed explanation of Razor syntax. For more information, please follow other related articles on the PHP Chinese website!