skip to content

Inside a Razor code block or @foreach body in a .cshtml view, how do @: and <text> output plain text that has no HTML tag?

level: middleimportance: should knowfreq 30%

answer

  1. inside braces C# is the default
  2. a leading tag switches automatically
  3. rest of the line
  4. a tag that never reaches the browser

basics

~20 s

Inside @{ } or a control structure Razor reads C#, so bare text breaks the build; @: renders the rest of one line as markup, and <text>…</text> renders only its contents, across lines if needed.

solid answer

~40 s

Inside a `@{ }` block or the braces of `@if` or `@foreach` the default language is C#. A line that starts with an HTML tag switches to markup on its own, but bare words such as `Item: @line.Sku` are parsed as C# and the build fails with errors like CS0103. Razor has two explicit transitions for that. `@:` renders the rest of the current line as markup. `<text>...</text>` marks a delimited run, which may span several lines, and only its contents are written: the `<text>` tag itself never reaches the browser, and no whitespace outside it is added. In both, `@` expressions still work and are HTML-encoded, so `@:Items in this order: @lines.Length` prints the count.

code

razor · 13 lines
razor
@{
    var lines = new[] { new Line("A-1", 2, 9.50m), new Line("B-2", 1, 20m) };
}
<p>
@foreach (var line in lines)
{
    <text>@line.Sku x@(line.Qty); </text>
}
</p>
@if (lines.Length > 1)
{
    @:Items in this order: @lines.Length
}

go deeper

for a junior

Remember that inside braces Razor reads C#, so bare text needs a tag, @: for one line, or <text> for a delimited run.

for a middle

Explain the implicit transition on a leading tag, the difference between @: and <text>, and that the <text> tag itself is never rendered.

for a senior

Read a failed view build that points at plain words and recognise a missing transition; pick <text> where exact whitespace matters, such as plain-text email bodies.

for a principal

When views keep needing many transitions, consider whether the text belongs in a dedicated template or helper rather than inside loops of C#.

## Why bare text fails inside braces In a `.cshtml` view or Razor Page, the top level of the file is HTML. Once you open a **code block** (`@{ ... }`) or the braces of a **control structure** (`@if`, `@foreach`, `@for`, `@switch` and the rest), the parser is reading **C#**. Razor has to decide, line by line, whether what follows is code or markup. It does that with one rule: a line that **starts with an HTML tag** switches to markup until the element closes. Anything else is C#. So a loop body like this ```razor @foreach (var line in lines) { Item: @line.Sku } ``` is read as the C# statement `Item: ...`. On the .NET 10 SDK the build fails with `CS0103: The name 'Item' does not exist in the current context` and `CS1002: ; expected`. The Razor docs call this a runtime error; in practice the compiler reports it at build time, because views are compiled with the project. ## The implicit transition: a line that starts with a tag Wrapping the text in a real element is the simplest fix when an element belongs there anyway: - `<li>@line.Sku</li>` inside `@foreach` renders an `<li>` per item; - `<p>Your order is empty.</p>` inside `@if` renders a paragraph. Inside that element `@` starts C# again, and the element closes the markup run. The fix is wrong when you need **no** element: comma-separated lists, text inside an existing paragraph, or a plain-text email body. For those Razor has two explicit transitions. ## `@:`: the explicit line transition `@:` tells the parser that **the rest of the current line** is markup. Everything after it up to the line break is written out, with `@` expressions evaluated and HTML-encoded: ```razor @if (lines.Length > 1) { @:Items in this order: @lines.Length } ``` On the .NET 10 SDK this wrote ` Items in this order: 2`: the indentation in front of `@:` went out too. Use `@:` for short, single-line text. ## `<text>`: the explicit delimited transition `<text>...</text>` is a **Razor-only tag**. It marks a run of markup that may span several lines, and only what sits between the tags is written: 1. the `<text>` and `</text>` tags themselves are not rendered; 2. no whitespace before or after the tag appears in the output; 3. `@` expressions inside still evaluate and are encoded. That makes it the tool for tight output from a loop. `<text>@line.Sku x@(line.Qty); </text>` inside `@foreach` renders `A-1 x2; B-2 x1; ` with nothing between the items. ## Choosing between them | Need | Use | Why | |---|---|---| | Text that belongs in an element | `<p>`, `<li>`, `<span>` | The tag switches to markup and is real HTML | | One line of bare text | `@:` | Shortest; ends at the line break | | Several lines, or exact whitespace | `<text>...</text>` | Delimited; the tag itself is not rendered | A related tool: a C# **local function** declared inside `@{ }` may contain markup, so `void RenderName(string name) { <p>@name</p> }` acts as a small template you call several times. ## Whitespace: why the choice shows in the output The two transitions differ most in what they do with spaces and line breaks, and that matters wherever the output is not forgiving HTML: - with `@:`, everything after the marker up to the line break is written, and on the .NET 10 SDK the indentation in front of the marker was written too; - with `<text>`, only the characters between the tags are written, so you control exactly where spaces and separators go; - a tag-based line such as `<li>...</li>` keeps the surrounding indentation and line breaks of the source, which HTML collapses but plain text does not. So for an HTML page either transition is fine; for a plain-text email or a CSV export, `<text>` gives the predictable result. ## Things that still apply - Encoding does not change: a value after `@:` or inside `<text>` is HTML-encoded exactly as elsewhere. - `@:` and `<text>` are for code contexts. At the top level of a view you are already in markup and need neither. - Plain-text output (such as a text email body rendered from a view) is where `@:` and `<text>` matter most, because there are no tags to trigger the implicit transition.

  • Are @ expressions after @: or inside <text> still evaluated and encoded?
    Yes. Both transitions switch the parser to markup, and in markup `@` starts C# again. So `@:Items: @lines.Length` writes the count, and `<text>@line.Sku</text>` writes the SKU, each HTML-encoded like any other Razor expression. Use `@( ... )` inside them for arithmetic or text glued to the value.
  • When is <text> a better choice than @: inside a Razor loop?
    When the text spans more than one line, or when whitespace matters. `@:` covers only the rest of one line and keeps the line's indentation, while `<text>` delimits a run of any length and adds no whitespace before or after the tag, which suits joining items such as `A-1 x2; B-2 x1;` on one line.

  • Plain text inside a Razor @foreach body is printed as markup automatically.
  • The <text> tag is sent to the browser like any other element.
  • @: renders only the next word, not the rest of the line.
  • @ expressions after @: are printed literally, not evaluated.
  • Bare text in a Razor code block only fails at request time, never at build.