๐งพ Razor โ Syntax Overview โ The Elegant Way to Write C# in HTML
๐งฒ Introduction โ What Is Razor Syntax in ASP.NET?
Razor syntax is a streamlined way to combine C# code with HTML markup inside .cshtml
pages in ASP.NET. It was designed to be lightweight, expressive, and easy to readโoffering a clean alternative to older .aspx
or Web Forms
markup with <% ... %>
.
๐ฏ In this guide, youโll learn:
- Core Razor syntax elements like
@
, code blocks, and expressions - Conditional statements, loops, and inline rendering
- Syntax rules for escaping, raw HTML, and layout helpers
- Tips and best practices for clean Razor code
๐ Razor Syntax Basics
Razor uses the @
symbol to switch between HTML and C#.
@{
var name = "Vaibhav";
var date = DateTime.Now;
}
<p>Hello, @name! Today is @date.ToLongDateString().</p>
๐งช Output:
Hello, Vaibhav! Today is Tuesday, June 11, 2025.
๐งฑ Razor Code Block
Wrap multiple C# lines inside @{ ... }
@{
int a = 5;
int b = 10;
int sum = a + b;
}
<p>Sum = @sum</p>
๐ค Razor Inline Expressions
Use @expression
to embed single-line values or methods:
<p>Time now: @DateTime.Now.ToShortTimeString()</p>
๐ Razor Loops and Conditionals
โ
if
, else
Conditional
@if (User.Identity.IsAuthenticated)
{
<p>Welcome, @User.Identity.Name!</p>
}
else
{
<p>Please log in.</p>
}
โ
for
Loop
@for (int i = 1; i <= 3; i++)
{
<li>Item @i</li>
}
โ
foreach
Loop
@foreach (var item in new[] { "Apple", "Banana", "Cherry" })
{
<p>@item</p>
}
๐ Razor Comments
@* This is a Razor comment and won't render in HTML *@
๐งฌ Razor Special Scenarios
โ
Escaping the @
Symbol
Use @@
to render a literal @
:
<p>Email: @@mycompany.com</p>
๐งช Output:
Email: @mycompany.com
โ
Raw HTML with @Html.Raw()
@{
var htmlText = "<strong>This is bold</strong>";
}
@Html.Raw(htmlText)
Without Raw
, Razor encodes the tag into plain text.
โ Multi-line Code + HTML Mix
@{
var colors = new[] { "Red", "Green", "Blue" };
}
<ul>
@foreach (var color in colors)
{
<li>@color</li>
}
</ul>
๐ Razor Syntax Summary Table
๐ฃ Element | ๐ง Description | โ Example |
---|---|---|
@{ } | Code block | @{ var x = 1; } |
@value | Inline output | @DateTime.Now |
@Html.Raw() | Output raw HTML | @Html.Raw("<b>Hi</b>") |
@* comment *@ | Razor comment | @* This is hidden *@ |
@if / else | Conditional rendering | @if(condition) { ... } |
@for / @foreach | Iteration over loops | @foreach(var i in list) { ... } |
@@ | Escape Razor syntax | @@symbol renders @ |
๐ Best Practices for Razor Syntax
โ Do:
- Use
@model
at the top for strongly typed views - Keep logic minimal in the
.cshtml
file (usePageModel
/Controller
) - Use HTML helpers and tag helpers for form fields and rendering
โ Avoid:
- Writing complex logic in views
- Using
Response.Write
(Razor handles output automatically) - Mixing JavaScript in Razor blocksโseparate script logic when possible
๐ Summary โ Recap & Next Steps
Razor syntax gives ASP.NET developers a simple, readable way to mix server-side logic with HTML. Its minimal syntax reduces clutter, improves maintainability, and provides full C# support for building dynamic views.
๐ Key Takeaways:
- Use
@
to switch between HTML and C# code - Razor supports
if
,for
,foreach
, and expressions inline - Razor is cleaner and more modern than older Web Forms markup
โ๏ธ Real-world Use Cases:
- Render dynamic product listings or tables
- Show/hide UI sections based on user roles
- Loop over lists and format output cleanly
โ FAQs โ Razor Syntax Overview
โ Is Razor syntax case-sensitive?
โ
Yes, Razor uses C# which is case-sensitive.
โ Can I use JavaScript inside Razor blocks?
โ
Yes, but it’s best to keep JavaScript outside @{}
blocks and embed it properly using <script>
tags.
โ What file types use Razor?
โ
.cshtml
(C#) and .vbhtml
(VB.NET)
โ How do I debug Razor syntax errors?
โ
Razor compilation errors will show during runtime. Use descriptive error messages and try to isolate the block causing failure.
Share Now :