


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!

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)