← All lessons

Level 1 / ASP.NET Core Razor / lesson 6 of 6

Code blocks, escapes, and the edge of the sandbox

Use an @{ } block, escape a literal @, and see what this workbench refuses to guess at.

`@{ }` runs C# without writing anything out. `@@` writes a literal at sign. And this preview is a simulator with a small supported subset - the last step is about learning where that edge is, because a tool that silently mis-renders your code is worse than one that stops.

Understand the idea

Execution is not output

@{ ... } is a C# code block; it can declare a local variable but does not itself emit its value. A later @updated expression writes that value into the markup.

The @ character has two jobs

A single @ begins Razor syntax; @@ emits a literal @. Escaping matters for visible handles and addresses so Razor does not try to evaluate a name.

Know which tool is answering

The browser preview is a constrained teaching simulator. A rejected expression means 'not supported here', not necessarily 'invalid in Razor'. Confirm real framework behavior with an actual build.

5 lines
7 lines

Preview - simulated. Not a .NET compiler.

Follow on the socials.

Step 1 of 3

In progress

Run C# without printing it

Add @{ var updated = "Checked this morning"; } then render @updated inside the first empty paragraph.

  • The markup renders @updated - not done yet
  • The preview shows Checked this morning - not done yet

Coach

The checklist above is what decides whether a step is done. The coach only explains; it never grades.

The preview is a simulated Razor renderer, not a compiler. It supports @model, @{ } literal assignments, @Expression, @foreach, @if/@else, @* comments *@, and plain markup with class, id, and title attributes. Anything else is reported as outside the lesson instead of being guessed at.

Where the simulator stops

A real Razor renderer supports far more C# than this exercise. Do not copy the simulator's restrictions into a real project as rules of the language.

Try it in a real .NET project

In a Razor Pages project, declare a local value inside @{ }, display it later, and render a literal @ in HTML. Try DateTime.Now in the real app to see the difference between Razor and the simulator.

Check your understanding

The preview rejects @DateTime.Now. Does that mean Razor Pages does too?

Show the explanation

No. The preview only simulates a restricted subset and explicitly refuses other syntax. Use a real ASP.NET Core project to learn what Razor itself supports.

Goose School teaches Microsoft's stack. Goose Solutions is not affiliated with, endorsed by, or sponsored by Microsoft.