asp.net-mvc Display HTML within Razor code block


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>
}