the symptom, in your words

"RZ1010 error from nested @{ } inside a Razor code block"

✓ verified on 26.07.08e
lane Building UIposted

▸ SYMPTOM

Adding a @{ var x = ...; } block inside an existing @if(...){ } or @for(...){ } block produces:

snippet
RZ1010: Unexpected "{" after "@" character

The code looks reasonable (you just wanted a local variable), but the Razor compiler rejects it.

▸ CAUSE

The @{ } syntax is a markup-to-code transition. It tells the Razor parser "switch from HTML context into C# context." But inside an @if or @for block, you are already in C# context. Writing @{ } there is a double-transition that the parser cannot resolve.

snippet
@if (showPanel)
{
    @{ var label = "Hello"; }   // RZ1010 - already in code context
    <span>@label</span>
}

▸ FIX

Option A. Hoist the setup block above the wrapping code block:

snippet
@{
    var label = showPanel ? "Hello" : "";
}
@if (showPanel)
{
    <span>@label</span>
}

Option B. Drop the @ and use a bare block (you're already in C#):

snippet
@if (showPanel)
{
    {
        var label = "Hello";
        // ... use label
    }
    <span>Done</span>
}

In most cases, option A is cleaner: hoist the variable declaration to before the conditional.

▸ WHY IT WORKS

Razor's parser tracks whether you're in markup or code context. @{ } is only valid as a transition from markup into code. Once you're inside @if { ... }, you're already in code: writing bare C# statements, var declarations, and method calls directly. The @ prefix is only needed to output an expression back into markup (e.g., <span>@label</span>), not to open a new code block.

Verified on engine 26.07.08e: seen in a real project.
s&box moves fast; an undated fix is a liability. Spot a stale detail?

Want to know when new guides or fixes drop? Join the community to help build this out. Report gotchas, flag outdated fixes, or just lurk.

Join the Discord