โœจ ASP.NET Razor Syntax
Estimated reading: 4 minutes 34 views

๐Ÿงพ 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; }
@valueInline output@DateTime.Now
@Html.Raw()Output raw HTML@Html.Raw("<b>Hi</b>")
@* comment *@Razor comment@* This is hidden *@
@if / elseConditional rendering@if(condition) { ... }
@for / @foreachIteration 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 (use PageModel/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 :

Leave a Reply

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

Share

๐Ÿงพ Razor โ€“ Syntax Overview

Or Copy Link

CONTENTS
Scroll to Top