Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET Razor syntax


May 12, 2021 ASP.NET


Table of contents


ASP.NET Razor - C# and VB code syntax


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

This section explains the syntax rules for Razor C# and Razor VB.


The main Razor C# syntax rule

  • The Razor code block is included in the . .
  • Inline expressions (variables and functions) begin with
  • The code statement ends with a sign
  • Variables are declared using the var keyword
  • Strings are enclosed in quotation marks
  • The case-sensitive code for C# is not the case
  • The extension of the C# file is .cshtml

An instance 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 + " Here in Huston it is: " + weekDay;
}

<p>The greeting is: @greetingMessage </p>

Run an instance . . .


The main Razor VB syntax rule

  • Razor code blocks are included in the @Code ... End Code
  • Inline expressions (variables and functions) begin with
  • Variables are declared using the Dim keyword
  • Strings are enclosed in quotation marks
  • VB code is case insenso sensitive
  • The extension of the VB file is .vbhtml
<!-- 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 & " Here in Huston it is: " & weekDay
End Code


<p>The greeting is: @greetingMessage </p>

Run an instance . . .


How does it work?

Razor is a simple programming syntax that embeds server code in a Web page.

The Razor syntax is based ASP.NET framework, and is designed to create parts of the web application Microsoft.NET framework.

Razor syntax 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 razor code and then sends the HTML page to the browser. C ode executed on the server can perform tasks that cannot be accomplished on a browser, such as accessing the server database. S erver code creates dynamic HTML content that is then sent to the browser. From a browser point of view, the HTML generated by the server code is no different from static HTML content.

The ASP.NET page with Razor syntax has a special file extension, cshtml (Razor C#) or vbhtml (Razor VB).


Use objects

Server encoding often involves objects.

The "Date" object is a typical built-in ASP.NET object, but the object can also be customized, a Web page, a text box, a file, a database record, and so on.

The object has a method for execution. A database record may have a "Save" method, an image object may have a "Rotate" method, an e-mail 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.

ASP.NET date object has a Now property (written as Date.Now) and the Now property has a Day property (written date.Now.Day). T he following example shows how to access some properties of a Data object:

<table border="1">
<tr>
<th width="100px">Name</th>
<td width="100px">Value</td>
</tr>
<tr>
<td>Day</td><td> @DateTime.Now.Day </td>
</tr>
<tr>
<td>Hour</td><td> @DateTime.Now.Hour </td>
</tr>
<tr>
<td>Minute</td><td> @DateTime.Now.Minute </td>
</tr>
<tr>
<td>Second</td><td> @DateTime.Now.Second </td>
</tr>
</td>
</table>

Run an instance . . .


If and Else conditions

An important feature of dynamic web pages is that you can decide what to do based on your criteria.

A common way to do this is to use if ... Else statement:

@{
var txt = "";
if(DateTime.Now.Hour > 12)
{txt = "Good Evening";}
else
{txt = "Good Morning";}
}
<html>
<body>
<p>The message is @txt </p>
</body>
</html>

Run an instance . . .


Read user input

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

The input is read through the Request?function, and the transfer of input data is determined by the IsPost condition:

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

<html>
<body style="background-color: beige; font-family: Verdana, Arial;">
<form action="" method="post">
<p><label for="text1">First Number:</label><br>
<input type="text" name="text1" /></p>
<p><label for="text2">Second Number:</label><br>
<input type="text" name="text2" /></p>
<p><input type="submit" value=" Add " /></p>
</form>
<p> @totalMessage </p>
</body>
</html>

Run an instance . . .