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

ASP.NET 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

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 + " Here in Huston it 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 & " Here in Huston it is: " & weekDay
End Code

The greeting is:

@greetingMessage

Run Example »


How does it work?

Razor is a simple programming syntax for embedding server code into web pages.

Razor syntax is based on the ASP.NET framework, specifically designed to create web applications which are part of the Microsoft .NET framework.

Razor syntax supports all the features of ASP.NET, but uses a simplified syntax that is easier for beginners to learn and more efficient for experts.

A Razor web page can be described as an HTML page with two types of content: HTML content and Razor code.

When the server reads the page, it first runs the Razor code, then sends the HTML page to the browser. Code executed on the server can perform tasks that cannot be done in the browser, such as accessing the server database. The server code can create dynamic HTML content and send it to the browser. From the browser's perspective, the HTML generated by the server code is no different from static HTML content.

ASP.NET web pages with Razor syntax have special file extensions: cshtml (Razor C#) or vbhtml (Razor VB).


Working with Objects

Server-side coding often involves objects.

The "Date" object is a typical built-in ASP.NET object, but objects can also be custom: a web page, a text box, a file, a database record, and so on.

Objects have methods for performing actions. A database record may have a "Save" method, an image object may have a "Rotate" method, an email object may have a "Send" method, and so on.
Objects also have properties that describe their characteristics. A database record may have FirstName and LastName properties.

The ASP.NET Date object has a Now property (written as Date.Now), and the Now property has a Day property (written as Date.Now.Day). The example below demonstrates how to access some properties of the Date object:

Example

Name Value
Day

@DateTime.Now.Day

Hour

@DateTime.Now.Hour

Minute

@DateTime.Now.Minute

Second

@DateTime.Now.Second

Run Example »


If and Else Conditions

An important feature of dynamic web pages is that you can decide what to do based on 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";}
}

The message is

@txt

Run Example »


Reading User Input

Another important feature of dynamic web pages is that you can read user input.

Input is read through the Request[] function, and the transmitted input data is determined by the IsPost condition:

Example

@{
var totalMessage = "";
if(IsPost)
{
var num1 = Request["text1"];
var num2 = Request["text2"];
var total = num1.AsInt() + num2.AsInt();
totalMessage = "Total = " + total;
}
}



@totalMessage

Run Example »