Table of contents
1.
Introduction
2.
Razor
3.
Important Razor Syntax Rules in C#
4.
Inline expressions
5.
Multi-statement Code block
6.
Text from Code Block
7.
if-else condition
8.
for loop
9.
Model
10.
Declare Variables
11.
Frequently Asked Questions
11.1.
What does Razor Support?
11.2.
What is Razor?
11.3.
What is the main difference between Razor pages and MVC?
11.4.
What are the advantages of razor pages?
11.5.
What file extension does Razor code have?
12.
Conclusion
Last Updated: Mar 27, 2024
Medium

ASP.NET Razor Code Expressions

Author Toohina Barua
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Razor syntax is a template markup syntax based on the C#  programming language that allows a programmer to interact with HTML. Instead of utilizing the ASP.NET Web Forms (.aspx) markup syntax to indicate code blocks with percent = percent > symbols, Razor syntax starts code blocks with a @ character and does not require explicit closing of the code-block. We are going to learn about Razor code expressions in detail in this article. So let’s dive in!

Razor

Razor is a markup syntax for embedding server-side code (Visual Basic and C#) in web pages.
While a web page is being written to the browser, server-based programming can build dynamic web content on the fly. When a web page is requested, the server runs the server-side code before returning the page to the browser. The programs can execute sophisticated functions, such as accessing databases, by operating on the server.
Razor is a web application development framework based on ASP.NET. It has all of the power of classic ASP.NET markup, but it's a lot easier to use and master.

Important Razor Syntax Rules in C#

Following are some rules for Razor Syntax:

  • @{ ... } is used to surround Razor code blocks.
  • @ is used to start inline expressions (variables and functions).
  • Semicolon marks the conclusion of a code statement.
  • The var keyword is used to declare variables.
  • Quote marks are used to encapsulate strings.
  • Case is important in C# programming.
  • The extension.cshtml is used for C# files.

Inline expressions

To write server-side C# or VB code with HTML code, begin with the @ symbol. To display the value of a server-side variable, such as DateTime, write @Variable Name. The current date and time are now returned. As a result, write @DateTime. Now, as seen below, display the current date and time. There is no need for a semicolon at the end of a single-line phrase.

<h1>Razor syntax demo</h1>
<h2>@DateTime.Now.ToShortDateString()</h2>

Output:

Razor syntax demo 
21-02-2022

Multi-statement Code block

Multiple lines of server-side code can be surrounded in braces @{ ... }. Each line, just like in C#, must end with a semicolon.

@{
    var date = DateTime.Now.ToShortDateString();
    var message = "Hello Ninjas!";
}

<h2>Today's date is: @date </h2>
<h3>@message</h3>

Output:

Today's date is: 21-02-2022
Hello Ninjas!

Text from Code Block

To show text within a code block, use @: or <text></text>.

@{
    var date = DateTime.Now.ToShortDateString();
    string message = "Hello Ninjas!";
    @:Today's date is: @date <br />
    @message                              
}

Output:

Today's date is: 21-02-2022
Hello Ninjas!

Use the <text> tag within a code block to display text, as shown below.

@{
    var date = DateTime.Now.ToShortDateString();
    string message = "Hello Ninjas!";
    <text>Today's date is:</text> @date <br />
    @message                              
}

Output:

Today's date is: 21-02-2022
Hello Ninjas!

if-else condition

Starting with the @ sign, write an if-else condition. Even for a single statement, the if-else code block must be surrounded in brackets {}.
The following code tells us whether the current year is a leap year or not. 

@if(DateTime.IsLeapYear(DateTime.Now.Year) )
{
    @DateTime.Now.Year @:is a leap year.
}
else {
    @DateTime.Now.Year @:is not a leap year.
}

Output:

2022 is not a leap year.

for loop

The following code demonstrates the use of Razor syntax in loops:

@for (int i = 0; i < 5; i++) {
    @i.ToString() <br />
}

Output:

0 
1 
2 
3 
4

Model

To utilize the model object anywhere in the view, use @model.

@model Student

<h2>Student Detail:</h2>
<ul>
    <li>Student Id: @Model.StudentId</li>
    <li>Student Name: @Model.StudentName</li>
    <li>Age: @Model.Age</li>
</ul>

Output:

Student Detail: 
- Student Id: 1 
- Student Name: John 
- Age: 22

Declare Variables

Declare a variable in a code block surrounded in brackets, then utilize it using the @ sign inside HTML.

@{
    string str = "";

    if(1 > 0)
    {
        str = "Hello Ninjas!";
    }
}

<p>@str</p>

Output:

Hello Ninjas!

Frequently Asked Questions

What does Razor Support?

Razor supports both C# (C sharp) and VB (Visual Basic)

What is Razor?

Razor is a markup syntax for embedding server-side code (Visual Basic and C#) in web pages. 

What is the main difference between Razor pages and MVC?

The main difference between Razor Pages and MVC is that the model and controller code is also contained within the Razor Page. In a nutshell, it's similar to an MVVM (Model-View-View-Model) framework. It allows for two-way data binding as well as a more straightforward development experience with separated concerns. 

What are the advantages of razor pages?

The main advantage of razor pages is that are better organized and single-responsible. In general, the files are better structured. Like the original ASP.NET WebForms, you have a Razor View and the full code behind a file.

Each app page is self-contained, with its own view and code structured together, making it less complex than the MVC.

What file extension does Razor code have?

The specific file extension cshtml (Razor using C#) or vbhtml (Razor using Visual Basic) is used for ASP.NET web pages with Razor syntax 

Conclusion

From this article, we learned about Razor code expressions and understood it using practical coding examples.

Apart from this, you can also expand your knowledge by referring to these articles on Features Of ASP Net and ASP Full Form.

But this is not enough; you need something extra to excel in web development truly. If you want to learn more about web development, you can read our articles or take our highly curated Web Development course.

Live masterclass