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

๐Ÿ“ฅ Razor โ€“ Introduction โ€“ Simplify Server-Side Coding in ASP.NET

๐Ÿงฒ Introduction โ€“ What Is Razor in ASP.NET?

Razor is a lightweight, server-side templating syntax used in ASP.NET to generate dynamic HTML markup using C# (or VB.NET). It blends server code with HTML using a clean and expressive syntax that avoids bulky markup and enhances productivity.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • What Razor is and why it matters in modern ASP.NET development
  • Basic syntax and how to embed C# in HTML
  • How Razor differs from classic .aspx and Web Forms
  • Practical examples of Razor expressions, loops, and conditionals

๐Ÿ’ก What Is Razor Syntax?

Razor syntax starts with the @ symbol, which indicates a switch from HTML to C# code. It allows you to embed logic, display values, and render dynamic content directly in the markup.

โœ… Razor files use the .cshtml (C#) or .vbhtml (VB.NET) extension.


๐Ÿงช Example โ€“ Basic Razor Syntax in .cshtml

@{
    var name = "Vaibhav";
    var time = DateTime.Now;
}

<h2>Welcome, @name!</h2>
<p>Current time is: @time</p>

๐Ÿ” Explanation:

  • @{ ... } is a Razor code block for declaring variables
  • @name and @time inject values into the HTML

๐Ÿงช Output:

Welcome, Vaibhav!
Current time is: 6/11/2025 4:45 PM

๐Ÿ“„ Razor Page Layout

A typical Razor Page includes:

  • A @page directive
  • A mix of C# and HTML
  • Page model support in PageModel.cs file (ASP.NET Core)
@page
@model IndexModel
@{
    ViewData["Title"] = "Home Page";
}
<h1>@ViewData["Title"]</h1>

๐Ÿ” Razor Loops and Conditionals

โœ… Conditional Statement

@if (User.Identity.IsAuthenticated)
{
    <p>Welcome, @User.Identity.Name!</p>
}
else
{
    <p>Please <a href="/login">login</a>.</p>
}

โœ… Loop Example

@for (int i = 1; i <= 5; i++)
{
    <p>Item @i</p>
}

๐Ÿงฉ Inline Razor vs Code Blocks

Syntax TypeExample
Inline<p>Hello, @name</p>
Code Block@{ var name = "John"; }
Expressions@DateTime.Now
HTML + Logic MixHTML tags wrapped in @if logic

๐Ÿง  Razor vs ASP.NET Web Forms (.aspx)

FeatureRazor SyntaxASPX Syntax
Code Injection@name<% = name %>
Control Flow@if, @for<% if(condition) { %>
File Extension.cshtml.aspx
LanguageC# only (in Razor Pages)VB.NET or C#
Layout SimplicityCleaner & compactVerbose markup-heavy syntax

โœ… Razor is designed for clarity, performance, and maintainability.


๐Ÿงฌ Razor Directives

DirectivePurpose
@pageMarks a Razor Page (used in ASP.NET Core)
@modelDeclares the view model
@usingImports a namespace
@inheritsSpecifies base class for page
@functionsDeclares helper methods

๐Ÿ“˜ Best Practices for Razor Pages

โœ… Do:

  • Keep logic minimal in views (use PageModel or Controller)
  • Use @Html.DisplayFor, @Html.EditorFor for model-driven UIs
  • Use @await for asynchronous method calls

โŒ Avoid:

  • Putting complex C# logic in .cshtml files
  • Mixing too much HTML inside @{ ... } blocks
  • Using ViewBag for strongly-typed models (prefer @model)

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Razor simplifies dynamic content generation in ASP.NET by mixing C# and HTML elegantly. Itโ€™s the preferred syntax in ASP.NET Core, Razor Pages, and MVC Views due to its readability and maintainability.

๐Ÿ” Key Takeaways:

  • Razor uses @ to switch from HTML to C#
  • Razor is cleaner and more modern than Web Forms
  • It supports full C# syntax, including loops and conditions

โš™๏ธ Real-world Use Cases:

  • Generating dynamic user dashboards
  • Templating product listings, tables, or charts
  • Building form-driven, model-bound UI in ASP.NET Core

โ“ FAQs โ€“ Razor Introduction


โ“ What is Razor used for in ASP.NET?
โœ… Razor is used to render HTML markup with embedded C# logic dynamically.


โ“ Is Razor only for ASP.NET Core?
โœ… No. Razor works in ASP.NET Web Pages, ASP.NET MVC, and ASP.NET Core.


โ“ Can I use JavaScript inside Razor pages?
โœ… Yes. Razor is only interpreted server-side. JS is served normally as part of HTML.


โ“ Whatโ€™s the file extension for Razor?
โœ… Razor views use .cshtml (C#) or .vbhtml (VB.NET).


Share Now :

Leave a Reply

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

Share

๐Ÿ“ฅ Razor โ€“ Introduction

Or Copy Link

CONTENTS
Scroll to Top