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 contained within @{ ... }
- Inline expressions (variables and functions) start with @
- Code statements end with a semicolon
- Variables are declared using the var keyword
- Strings are enclosed in quotes
- C# code is case-sensitive
- The file extension for C# is .cshtml
C# Example
@{ var myMessage = "Hello World"; }
@myMessage
@{
var greeting = "Welcome to our site!";
var weekDay = DateTime.Now.DayOfWeek;
var greetingMessage = greeting + " Here in Huston it is: " + weekDay;
}
@greetingMessage
Main Razor VB Syntax Rules
- Razor code blocks are contained within @Code ... End Code
- Inline expressions (variables and functions) start with @
- Variables are declared using the Dim keyword
- Strings are enclosed in quotes
- VB code is not case-sensitive
- The file extension for VB is .vbhtml
VB Example
@Code dim myMessage = "Hello World" End Code
@myMessage
@Code
dim greeting = "Welcome to our site!"
dim weekDay = DateTime.Now.DayOfWeek
dim greetingMessage = greeting & " Here in Huston it is: " & weekDay
End Code
@greetingMessage
How It Works
Razor is a simple programming syntax for embedding server code in webpages.
Razor syntax is based on the ASP.NET framework, which is part of the Microsoft .NET framework aimed at creating web applications.
Razor supports all ASP.NET features but uses a simplified syntax that is easier for beginners to learn and more efficient for experts.
Razor pages can be described as HTML pages 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. Server-side code can perform tasks that cannot be done in the browser, such as accessing a server database. Server code can create dynamic HTML content and send it to the browser. From the browser's perspective, HTML generated by server code looks the same as static HTML.
ASP.NET pages with Razor syntax have special file extensions: .cshtml (Razor C#) or .vbhtml (Razor VB).
Working with Objects
Server coding often involves objects.
The "Date" object is a typical built-in ASP.NET object, but objects can also be custom, such as a webpage, a text box, a file, a database record, etc.
Objects have methods for performing actions. 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.
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 following example demonstrates how to access some properties of the Date object:
Example
@DateTime.Now.Day
@DateTime.Now.Hour
@DateTime.Now.Minute
@DateTime.Now.Second
If and Else Conditions
An important feature of dynamic webpages is that you can decide what to do based on conditions.
A common way to do this is by using if ... else statements:
Example
@{
var txt = "";
if(DateTime.Now.Hour > 12) {
txt = "Good Evening";
} else {
txt = "Good Morning";
}
}
<html>
@txt
</html>
Reading User Input
Another important feature of dynamic webpages is that you can read user input.
Input is read using the Request[] function, and input data is processed under 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