ASP.NET Tutorial
Estimated reading: 4 minutes 49 views

โœจ 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 โ€“ IntroductionOverview of Razor and its benefits in ASP.NET
๐Ÿงพ Razor โ€“ Syntax OverviewKey syntax rules, file types, and inline code
๐Ÿ”ค Razor C# โ€“ VariablesHow to declare and print C# variables in Razor
๐Ÿ” Razor C# โ€“ LoopsUsing for, foreach, and while inside views
๐Ÿ”ฃ Razor C# โ€“ LogicWriting if, else, and switch blocks
๐Ÿ”ค Razor VB โ€“ VariablesUsing VB.NET variables in Razor pages
๐Ÿ” Razor VB โ€“ LoopsVB For, For Each, Do While examples in views
๐Ÿ”ฃ Razor VB โ€“ LogicConditional 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

FeatureRazor C#Razor VB
Code Start@@
Code Block@{ }@Code ... End Code
Print Value@variable@variable
HTML OutputHTML mixed with @codeSame
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 .cshtml and .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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

โœจ ASP.NET Razor Syntax

Or Copy Link

CONTENTS
Scroll to Top