As soon as you start tag in Razor, it switches to markup (including <script>
). If you don't need any tag, use <text>
instead.
If you need quick copy/paste solution, take Tim's or Adam's as both do the job. However, as Razor uses pretty complex algorithms while parsing mix of several languages, it is worth providing Phil Haack's Razor part of quick reference in its entirety (allowed by CC-BY license):
Code Block:
@{
int x = 123;
string y = "because.";
}
Expression (Html Encoded):
<span>@model.Message</span>
Expression (Unencoded):
<span>
@Html.Raw(model.Message)
</span>
Combining Text and markup:
@foreach(var item in items) {
<span>@item.Prop</span>
}
Mixing code and Plain text:
@if (foo) {
<text>Plain Text</text>
}
Mixing code and plain text (alternate):
@if (foo) {
@:Plain Text is @bar
}
Email Addresses:
Hi philha@example.com
Explicit Expression:
<span>ISBN@(isbnNumber)</span>
Escaping the @ sign:
<span>In Razor, you use the
@@foo to display the value
of foo</span>
Server side Comment:
@*
This is a server side
multiline comment
*@
Calling generic method:
@(MyClass.MyMethod<AType>())
Mixing expressions and text:
Hello @title. @name.
I left out Razor delegate since there are Razor helpers now.