In asp.net mvc, Razor view engine is syntax that allow us to write server side code in view page. It help to combine the server side with HTML in a fluid manner. If you know c#, vb.net and bit HTML language then you can easily use razor in html file because Razor supports C# and visual basic programming language.
Syntax :
In Razor view engine, Razor code is enclosed within @{....}. Here @ character sign is tell the beginning of Razor code. This code can be a single line or a block of statements. Razor view engine syntax is as like below,
@{
// code for Razor block
}
In the above syntax '@{' is used to beginning of Razor code and '}' is used to end of the code.
Types of Block Statement in Razor :
In Razor, we have generally two type of block statement that we used that is as follow,
1- Single statement block and Inline expression:
As we know that the Razor code is write within @{...}, then the following code is represents the single statement block,
@{ var num=4; }
@{ var Message = 'Hello! How are you?;' }
<p> Number is : @num </p>
<p> Message is : @Message </p>
2- Multiple block statement and inline expression :
2- Multiple statement block and Inline expression:
In multiple statement block, all Razor variables are defines in a single @{...} and we need to end all variable line with semicolon( ; ). Multiple statement code block is shown as like,
@{
Var num=4;
var Message = 'This is a Razor message';
var weekDay = DateTime.Now.DayOfWeek;
}
<p> Today is : @weekDay </p>
Generally, In view page Razor engine code write at the top portion so that Razor code is accessible everywhere in that page. If we declare a Razor variable in middle of view page and access it at top position then it thrown error as like bellow code,
@{
ViewBag.Title = 'Index';
}
@{
@i; // This thrown an error - cannot use local variable 'i' before it is declared.
}
@{
int i = 24
}
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Razor View Engine :
In asp.net mvc, Razor view engine is syntax that allow us to write server side code in view page. It help to combine the server side with HTML in a fluid manner. If you know c#, vb.net and bit HTML language then you can easily use razor in html file because Razor supports C# and visual basic programming language.
Syntax :
In Razor view engine, Razor code is enclosed within @{....}. Here @ character sign is tell the beginning of Razor code. This code can be a single line or a block of statements. Razor view engine syntax is as like below,
@{// code for Razor block
}
In the above syntax '@{' is used to beginning of Razor code and '}' is used to end of the code.
Types of Block Statement in Razor :
In Razor, we have generally two type of block statement that we used that is as follow,
1- Single statement block and Inline expression:
As we know that the Razor code is write within @{...}, then the following code is represents the single statement block,
@{ var num=4; }@{ var Message = 'Hello! How are you?;' }
<p> Number is : @num </p>
<p> Message is : @Message </p>
2- Multiple block statement and inline expression :
2- Multiple statement block and Inline expression:
In multiple statement block, all Razor variables are defines in a single @{...} and we need to end all variable line with semicolon( ; ). Multiple statement code block is shown as like,
@{Var num=4;
var Message = 'This is a Razor message';
var weekDay = DateTime.Now.DayOfWeek;
}
<p> Today is : @weekDay </p>
Generally, In view page Razor engine code write at the top portion so that Razor code is accessible everywhere in that page. If we declare a Razor variable in middle of view page and access it at top position then it thrown error as like bellow code,
@{ViewBag.Title = 'Index';
}
@{
@i; // This thrown an error - cannot use local variable 'i' before it is declared.
}
@{
int i = 24
}