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
For the question about @RazorCodePart1 @@ @RazorCodePart2
, you need to the sequence:
@RazorCodePart1 @:@@ @RazorCodePart2
I know, it looks a bit odd, but it works and will get you the literal character '@' between the code blocks.
@@
should do it.
@@ 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.
I just had the same problem. I declared a variable putting my text with the @.
@{
var twitterSite = "@MyTwitterSite";
}
...
<meta name="twitter:site" content="@twitterSite">
I know this question is old, but I tried all of the above and it didn't help me escape the character "@" in ASP.NET full framework (MVC 5) inside a URL. Based on Terje Solem's answer though, the UTF-8 code %40
worked for me. this is the original URL I was trying to reach:
https://unpkg.com/@google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js
and with with the code in it:
https://unpkg.com/%40google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js
@Html.Raw("@")
seems to me to be even more reliable than @@
, since not in all cases @@
will escape.
Therefore:
<meta name="twitter:site" content="@twitterSite">
would be:
<meta name="twitter:site" content="@Html.Raw("@")twitterSite">