Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET Web Pages Razor

ASP.NET Web Pages - Adding Razor Code


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


What is Razor?


Adding Razor Code

Remember the web page from the previous chapter's example:

Web Pages Demo

Hello Web Pages

Now add some Razor code to the example:

Example

Web Pages Demo

Hello Web Pages

The time is

@DateTime.Now

Run Example »

The page contains ordinary HTML markup, and in addition, an @-marked Razor code has been added.

Razor code can perform all 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

C# Example

@{ var myMessage = "Hello World"; }

The value of myMessage is:

@myMessage

@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Today is: " + weekDay;
}

The greeting is:

@greetingMessage

Run Example »


Main Razor VB Syntax Rules

Example

@Code dim myMessage = "Hello World" End Code

The value of myMessage is:

@myMessage

@Code
dim greeting = "Welcome to our site!"
dim weekDay = DateTime.Now.DayOfWeek
dim greetingMessage = greeting & " Today is: " & weekDay
End Code

The greeting is:

@greetingMessage

Run Example »


More About C# and Visual Basic

If you want to learn more about the Razor, C#, and Visual Basic programming languages, please see the Razor section。