Home > Backend Development > C#.Net Tutorial > Share ASP.NET study notes (1)--WebPages Razor

Share ASP.NET study notes (1)--WebPages Razor

零下一度
Release: 2017-05-24 17:56:08
Original
1984 people have browsed it

In this tutorial, we will use Razor markup with C# and Visual Basic code.

What is Razor?

Razor is a markup syntax for adding server-based code to web pages

Razor has the functionality of traditional ASP.NET markup, but is easier to use and easier to learn

Razor is a server-side markup syntax, much like ASP and PHP

Razor supports C# and Visual Basic programming languages

Add Razor code

Remember the above The web page in the example chapter:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Web Pages Demo</title>
</head>
<body>
<h1>Hello Web Pages</h1>
</body>
</html>
Copy after login

Now add some Razor code to the example:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Web Pages Demo</title>
</head>
<body>
<h1>Hello Web Pages</h1> 
<p>The time is @DateTime.Now</p>
</body>
</html>
Copy after login

The page contains normal HTML markup, but otherwise , and also adds an @-marked Razor code.

Razor code can complete a variety of actions on the server in real time and display the results. (You can specify formatting options, otherwise only the default items are displayed.)

Main Razor C# syntax rules

Razor code blocks are enclosed in @{ ... }

Inline expressions (variables and functions) start with @

Code statements end with semicolon

Variables are declared using the var keyword

Strings are enclosed in quotes

C# Code is case sensitive

C# The file extension is .cshtml

C# Example

<!-- 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 + " Today is: " + weekDay;
}
<p>The greeting is: @greetingMessage</p>
Copy after login

Main Razor VB syntax rules

Razor code blocks are contained in @Code ... End Code

Inline expressions (variables and functions) start with @

Variables are declared using the Dim keyword

Strings are enclosed in quotation marks

VB code is not case-sensitive

The extension of the VB file 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 & " Today is: " & weekDay
End Code 
<p>The greeting is: @greetingMessage</p>
Copy after login

[Related recommendations 】

1. ASP.NET free video tutorial

2. Share ASP.NET study notes--WebPages introduction

4. What is ASP.NET MVC? Summary of ASP.NET MVC

5. In-depth understanding of the differences between ASP.NET MVC and WebForm

The above is the detailed content of Share ASP.NET study notes (1)--WebPages Razor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template