asp.net-mvc Razor Display HTML within Razor code block

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

While inside a Razor code block, the browser will only recognize HTML code if the code is escaped.

Use @: for a Single line:

@foreach(int number in Model.Numbers)
{
    @:<h1>Hello, I am a header!</h1>
}

Use <text> ... </text> for Multi-line:

@{
    var number = 1;

    <text>
        Hello, I am text
        <br / >
        Hello, I am more text!
    </text>
}

Note that Razor, when inside a code block, will understand HTML tags. Therefore, adding the text tag around HTML tags is unnecessary (although still correct), such as:

@{
    var number = 1;
    <text>
        <div>
            Hello, I am text
            <br / >
            Hello, I am more text!
        </div>
    </text>
}


Got any asp.net-mvc Question?