โœจ ASP.NET Razor Syntax
Estimated reading: 3 minutes 294 views

Razor C# โ€“ Logic โ€“ Control Flow with Conditionals in Razor Views

Introduction โ€“ What Is Razor Logic in ASP.NET?

Razor C# logic refers to the use of control flow statements such as if, else, switch, and ternary operators within Razor views. These allow developers to render dynamic UI based on conditions, flags, or user state, giving your .cshtml pages intelligent behavior without bloating the code.

In this guide, youโ€™ll learn:

  • How to write conditional logic in Razor views
  • How to use if, else, else if, switch, and ternary operations
  • How to mix logic with clean HTML output
  • Best practices for separating view logic from business logic

Razor if Statement

@{
    bool isLoggedIn = true;
}

@if (isLoggedIn)
{
    <p>Welcome back, user!</p>
}

Output:
If isLoggedIn is true โ†’ shows: Welcome back, user!


if...else and else if Example

@{
    int score = 85;
}

@if (score >= 90)
{
    <p>Grade: A</p>
}
else if (score >= 80)
{
    <p>Grade: B</p>
}
else
{
    <p>Grade: C or below</p>
}

Output:
Grade: B


Using switch in Razor

@{
    string role = "Admin";
}

@switch (role)
{
    case "Admin":
        <p>You have full access.</p>
        break;
    case "Editor":
        <p>You can edit content.</p>
        break;
    default:
        <p>Read-only access.</p>
        break;
}

Output:
You have full access.


โ” Ternary Operator in Razor

@{
    bool isPremium = false;
}

<p>Account Type: @(isPremium ? "Premium" : "Standard")</p>

Output:
Account Type: Standard


Logic in HTML Attributes

<input type="text" class="form-control @(hasError ? "is-invalid" : "")" />

Adds is-invalid class conditionally for form validation styling.


Combining Logic with Loops

@{
    var prices = new[] { 100, 200, 300 };
}

<ul>
@foreach (var price in prices)
{
    if (price > 150)
    {
        <li>High: @price</li>
    }
    else
    {
        <li>Low: @price</li>
    }
}
</ul>

Output:

  • Low: 100
  • High: 200
  • High: 300

Best Practices for Razor Logic

Do:

  • Use @if, @switch, and @for for clean UI logic
  • Keep business rules in your controller or PageModel
  • Use ternary only for short, readable conditions

Avoid:

  • Writing deeply nested if statements in Razor
  • Embedding database or calculation logic in .cshtml files
  • Mixing markup and complex logic in a single line

Summary โ€“ Recap & Next Steps

Razorโ€™s C# logic features let you control UI output dynamically and conditionally. From if blocks to switch statements, Razor simplifies rendering different content based on application state or model data.

Key Takeaways:

  • Use @if, @else, and @switch for conditional output
  • Ternary operators are useful for inline HTML attributes
  • Keep complex logic in controller or backend

Real-world Use Cases:

  • Show/hide UI based on login status
  • Format prices, badges, or roles conditionally
  • Highlight validation errors dynamically

FAQs โ€“ Razor Logic in Views


Can I write full C# logic in Razor?
Yes, Razor supports full C# syntax like if, for, switch, and foreach inside views.


Should I do heavy logic in .cshtml?
No. Keep Razor logic light. Use controllers or view models for complex conditions.


Is Razor logic secure?
Razor executes on the server, so users cannot view the logic. Only the final HTML output is sent to the browser.


Share Now :
Share

๐Ÿ”ฃ Razor C# โ€“ Logic

Or Copy Link

CONTENTS
Scroll to Top