Latest web development tutorials

ASP.NET Razor syntax

Razor also supports C # (C sharp) and VB (Visual Basic).


The main Razor C # syntax rules

  • Razor code blocks included in the @ {...}
  • Inline expressions (variables and functions) start with!
  • Code statements end with a semicolon
  • Variables declared using the var keyword
  • String in quotes
  • C # code is case sensitive
  • C # file extension is .cshtml

Examples 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>

Running instance »


The main Razor VB syntax rules

  • Razor code blocks contain @Code ... End Code in
  • Inline expressions (variables and functions) start with!
  • Variables declared using the Dim keyword
  • String in quotes
  • VB code is not case-sensitive
  • VB file extension is .vbhtml

Examples

<!-- 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>

Running instance »


How does it work?

Razor is a server code embedded in Web pages simple programming syntax.

Razor syntax is based on ASP.NET framework for creating Web applications specifically part of Microsoft.NET framework.

Razor syntax supports all the features of ASP.NET, but using a simplified syntax is easier to learn for beginners, experts in terms of more efficient.

Razor HTML web pages can be described with the following two types of content: HTML content and Razor code.

When the server reads the page, it first runs Razor code, and then send the HTML page to the browser. Code is executed on the server can execute some of the browser can not complete the task, for example, to access the database server. The server code can create dynamic HTML content, and then sent to the browser. From the point of view browser, server-generated HTML code is no different from static HTML content.

ASP.NET pages with Razor syntax have a special file extension cshtml (Razor C #) or vbhtml (Razor VB).


user target audience

Server coding often involves objects.

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

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 have attributes used to describe their own characteristics. A database record may have FirstName and LastName attributes.

Now ASP.NET Date object has a property (written Date.Now), Now there is a Day property attribute (written Date.Now.Day). The following example demonstrates how to access the Data object some attributes:

Examples

<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>

Running instance »


If and Else Condition

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

Common way to do this is to use the if ... else statement:

Examples

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

Running instance »


Read user input

Another important feature of dynamic pages, you can read user input.

Enter through the Request [] function to read and transmit data is input through IsPost determine the conditions:

Examples

@{
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>

Running instance »