In previous chapter, we have learnt about different view engines. In this chapter, we are going learn about how to do programming with the Razor syntax.
What is razor syntax?
As we all know, with ASP.NET MVC3 Microsoft has introduced new view engine, Razor, which was developed to simplify earlier aspx view engine syntax. Here you can write things @character, which called magic character in razor syntax. In razor view engine, you have .cshtml and .vbhtml for view. Where .cshtml is for C# language and .vbhtml is for VB.NET language.
Fundamentals of razor syntax:

You can also write single line statement and variable declaring with @{} syntax as shown below:
@{ var number = 10; }
Number is: @number
It will print number in browser as below:

- Same way you can also have multiline syntax with @{} syntax like following.
@{
var firstName = "jalpesh";
var lastName="vadgama";
var nameMessage = string.Format("My full name is : {0} {1}", firstName, lastName);
}
My name is :@nameMessage
It will print the name as shown below:

- You can also print a path with “\” character. It supports escape character also.
@{ var filePath = @"C:\ApplicationFolder\"; }
The file path is: @filePath
It will print filepath in browser as follows:

Therefore, from all above syntax you can see @ is a magic character for the razor syntax and you can print anything with @character.
Advance syntaxes for razor:
HTML Expressions in razor syntax:
Like other view engine, @razor syntax also supports the HTML expression seamless. You can write composite code with html tags and css etc. @ character with razor syntax supports it all.
Some of the examples are:
@{ var name = "Jalpesh"; }
The Name: @name
It will print name in italic characters.
IF and Else loop with razor syntax:
Like any other programming language Razor syntax also supports the ‘if and else’ loop. We can write if and loop like following.
@{
var message = string.Empty ;
if(IsPost)
{
message = "Page was postback";
}
else
{
message = "Page was not posback";
}
}
Posback Status:@message
As you can see in the above code I have checked IsPost with ‘if and else’ loop and assigning a message-to-message variable. In addition, I have a button for postback. Also, I have taken a form with post method on a submit button once we click it will post back the form. So first, we run application without postbacking the form. It will print message in browser like following:

Now once you click on submit button it will look like following:

@for(var i = 1; i < 10; i++)
{
Number @i
}
This will print number like following in browser:

@foreach (var variables in Request.ServerVariables)
{
}
It will print server variables name like following.

@{
var number=1; var message = string.Empty;
switch(number)
{
case 1:
message = "Number is 1";
break;
case 2:
message = "Number is 2";
break;
default:
message = "Number is 0";
break;
}
@message
}
It will print case 1 message as we have number assigned to 1 in browser like following:

Commenting in Razor Syntax:
Like any other languages, Razor syntax also have comments. Following are the syntaxs for single line and multi-line comments.
@* single line comment. *@
@*
Multi - Line
Comment
*@
That is it. As you can see Razor syntax is one of easiest syntax to learn. Hope you liked it. Stay tuned for next chapter. In next chapter, we are going to learn about entity framework and how we can use that in ASP.NET MVC.