</>
Skip to content
ASP lessons (31/40)

ASP — ASP.NET Razor

Razor Syntax

@{
    var name = "John";
    var items = new List<string> { "A", "B", "C" };
}

<h1>Hello, @name!</h1>

<ul>
@foreach (var item in items)
{
    <li>@item</li>
}
</ul>

Expressions

<p>@(2 + 2)</p>
<p>@DateTime.Now</p>
<p>@Model.Name</p>

Code Blocks

@{
    var total = 0;
    foreach (var item in Model.Items)
    {
        total += item.Price;
    }
}
<p>Total: @total</p>

Conditional

@if (Model.IsAdmin)
{
    <a href="/admin">Admin Panel</a>
}
else
{
    <p>Access denied</p>
}

Layout

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Mini Practice

  1. Use Razor expressions
  2. Create loops
  3. Use conditionals
  4. Set layouts

Up Next

Continue with ASP.NET Core — ASP.NET Core.

Related Topics

Frequently Asked Questions about ASP.NET Razor

What is ASP.NET Razor in ASP?

ASP.NET Razor is a fundamental concept in ASP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn ASP.NET Razor?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn ASP.NET Razor.

Why is ASP.NET Razor important in ASP?

ASP.NET Razor is essential for ASP development. Understanding this concept will help you write better code and solve real-world problems more effectively.