โจ ASP.NET Razor Syntax โ Code Integration with HTML Using C# & VB
๐งฒ Introduction โ What is Razor Syntax in ASP.NET?
Razor is a lightweight markup syntax used in ASP.NET (especially Web Pages and MVC) to seamlessly embed server-side code into HTML. Itโs clean, concise, and supports both C# and VB.NET, making dynamic web page generation simple and efficient.
๐ฏ In this guide, youโll learn:
- How Razor works and its syntax rules
- How to declare and use variables using Razor
- How to write loops and conditionals inside Razor views
- How Razor handles both C# and VB.NET logic
๐ Topics Covered
| ๐น Topic | ๐ Description |
|---|---|
| ๐ฅ Razor โ Introduction | Overview of Razor and its benefits in ASP.NET |
| ๐งพ Razor โ Syntax Overview | Key syntax rules, file types, and inline code |
| ๐ค Razor C# โ Variables | How to declare and print C# variables in Razor |
| ๐ Razor C# โ Loops | Using for, foreach, and while inside views |
| ๐ฃ Razor C# โ Logic | Writing if, else, and switch blocks |
| ๐ค Razor VB โ Variables | Using VB.NET variables in Razor pages |
| ๐ Razor VB โ Loops | VB For, For Each, Do While examples in views |
| ๐ฃ Razor VB โ Logic | Conditional logic in Razor using VB syntax |
๐ฅ Razor โ Introduction
Razor syntax allows embedding server-side code in .cshtml (C#) or .vbhtml (VB.NET) pages.
โ Example in Razor C#:
<h1>Hello @Model.UserName</h1>
โ Example in Razor VB:
<h1>Hello @Model("UserName")</h1>
Razor improves readability by minimizing code tags (@ vs <% %> from classic ASP).
๐งพ Razor โ Syntax Overview
| Feature | Razor C# | Razor VB |
|---|---|---|
| Code Start | @ | @ |
| Code Block | @{ } | @Code ... End Code |
| Print Value | @variable | @variable |
| HTML Output | HTML mixed with @code | Same |
| Comments | @* comment *@ | @* comment *@ |
โ
.cshtml = C# code
โ
.vbhtml = VB.NET code
๐ค Razor C# โ Variables
@{
var name = "Alice";
int age = 25;
}
<p>@name is @age years old.</p>
โ
Variables are scoped within @{ } blocks and rendered using @.
๐ Razor C# โ Loops
@for (int i = 0; i < 3; i++) {
<p>Item @i</p>
}
@foreach (var user in Model.Users) {
<li>@user.Name</li>
}
โ
Use for, foreach, and while as in regular C# code.
๐ฃ Razor C# โ Logic
@if (Model.IsAdmin) {
<p>Welcome, admin!</p>
} else {
<p>Access restricted.</p>
}
@switch (Model.Role) {
case "Admin":
<p>Admin Panel</p>;
break;
default:
<p>User Panel</p>;
break;
}
โ Razor makes conditional rendering intuitive with clean syntax.
๐ค Razor VB โ Variables
@Code
Dim name As String = "Bob"
Dim age As Integer = 30
End Code
<p>@name is @age years old.</p>
โ
VB developers can write Razor using familiar Dim, Integer, etc.
๐ Razor VB โ Loops
@Code
For i As Integer = 1 To 3
Response.Write("<p>Number " & i & "</p>")
Next
End Code
โ
VB-style loops work seamlessly within .vbhtml.
๐ฃ Razor VB โ Logic
@Code
If IsPost Then
Response.Write("<p>Form submitted.</p>")
Else
Response.Write("<p>Submit the form.</p>")
End If
End Code
โ
Familiar If...Else logic supported directly.
๐ Summary โ Recap & Next Steps
Razor syntax makes ASP.NET views cleaner, faster, and more maintainable by merging server-side code and HTML in one seamless flow. Whether using C# or VB.NET, you get dynamic capabilities with minimal syntax clutter.
๐ Key Takeaways:
- Razor uses
@to denote server-side logic - Supports both
.cshtmland.vbhtml - Inline and block-level code supported
- Clean syntax for loops, conditions, and output
- Fewer brackets and tags than classic ASP
โ๏ธ Real-World Applications:
- Dynamic dashboards and reports
- Form-driven UI rendering
- Email template generation
- User-specific conditional UIs
โ Frequently Asked Questions
โ What is the difference between Razor and classic ASP.NET syntax?
โ
Razor uses @, making it cleaner and more HTML-friendly. Classic used <% %> tags.
โ Can I use both C# and VB.NET Razor in one project?
โ
No. Razor pages are either .cshtml (C#) or .vbhtml (VB), and must not mix languages in one view.
โ Does Razor work in ASP.NET Core?
โ
Yes. Razor syntax is used in Razor Pages, Blazor, and MVC views in ASP.NET Core.
โ Can I include Razor logic inside HTML tags?
โ
Yes. Example:
<p class="@Model.CssClass">Text</p>
Share Now :
