I am creating a sample ASP.NET MVC 3 site using Razor as view engine. The razor syntax starts with @
character e.g. @RenderBody()
. If I write @test on my cshtml page it gives me parse error
CS0103: The name 'test' does not exist in the current context
How do I escape '@' character?
just add a variable in CSHTML file
var myVariable = @"@";
and add it to your layout
<span class="my-class"><a href="@myVariale" target="_blank" >link text</a></span>
Instead of HTML entity I prefer the use of @Html.Raw("@")
Razor @ escape char to symbols...
or
@@ is the escape character for @ in Razor views as stated above.
Razor does however try to work out when an '@' is just an '@' and where it marks C# (or VB.Net) code. One of the main uses for this is to identify email addresses within a Razor view - it should not be necessary to escape the @ character in an email address.
You can use @@ for this purpose. Like
var email = firstName + '\@@' + domain;
@Html.Raw("@")
seems to me to be even more reliable than@@
, since not in all cases@@
will escape.Therefore:
would be: