Latest web development tutorials

ASP.NET Web Pages Razor

In this tutorial, we will use the Razor mark C # and Visual Basic code.


What's Razor?

  • Razor is a server-based add the code to the web page markup syntax
  • Razor traditional ASP.NET markup features, but it is easier to use and easier to learn
  • Razor is a server-side markup syntax, and ASP and PHP like
  • Razor support C # and Visual Basic programming language

Add Razor Code

Remember that the last chapter example pages:

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Web Pages Demo</title>
</head>
<body>
<h1>Hello Web Pages</h1>
</body>
</html>

Now add some code to the Razor instance:

Examples

<!DOCTYPE html>

<html lang="en">
<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>

Running instance »

This page contains common HTML tags, in addition, the code also adds a Razor @ identity.

Razor code is able to complete some more action in real time on the server, and the results displayed. (You can specify formatting options, otherwise it will display the default entries.)


The main Razor C # syntax rules

  • Razor code blocks included in the @ {...}
  • Inline expressions (variables and functions) start with!
  • Code statements end with a semicolon
  • Variables declared using the var keyword
  • String in quotes
  • C # code is case sensitive
  • C # file extension is .cshtml

Examples of C #

<!-- 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>

Running instance »


The main Razor VB syntax rules

  • Razor code blocks contain @Code ... End Code in
  • Inline expressions (variables and functions) start with!
  • Variables declared using the Dim keyword
  • String in quotes
  • VB code is not case-sensitive
  • VB file extension is .vbhtml

Examples

<!-- 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>

Running instance »


More about C # and Visual Basic

If you want to learn more about Razor, C #, Visual Basic programming language, see this tutorial Razor section .