692

I am creating a sample ASP.NET MVC 3 site using Razor as the view engine. The Razor syntax starts with a @ character, e.g., @RenderBody(). If I write @test on my .cshtml page, it gives me a parse error

CS0103: The name 'test' does not exist in the current context

How do I escape the '@' character?

2
  • 1
    In C#, you can mark keywords with an @ to treat them as variable names rather than keywords. With all I know, it seems impossible to use reserved keywords in Razor this way. Commented Jul 15, 2013 at 11:48
  • 1
    @GrimaceofDespair the way to still mark keywords as variables is to enclose the second @ in braces. I managed to get this working by writing it this way: @(@new) Commented Oct 17, 2018 at 22:25

16 Answers 16

1124

@@ should do it.

Sign up to request clarification or add additional context in comments.

7 Comments

This doesn't seem to work in this case: @RazorCodePart1 @@ @RazorCodePart2 e.g. a literal @ between two Razor code snippets.
The best solution would be using the HTML-encoded character string for the @-character: @
I've got a <style> tag in my razor page, which has to embed an '@media {}' directive, so html encoding is not an option; only @@ works.
@WoIIe not in a URL.
In href use @("@")
|
194

Razor @ escape character to symbols...

<img src="..." alt="Find me on Twitter_ as @("@username")" /> 

or

<img src="..." alt="Find me on twitter as @("@")username" /> 

1 Comment

This method seems the best as it will also work for @media css stuff, whereas the HTML entity way probably will not.
52

@Html.Raw("@") seems to be even more reliable to me than @@, since @@ will not escape in all cases.

Therefore:

<meta name="twitter:site" content="@twitterSite"> 

would be:

<meta name="twitter:site" content="@Html.Raw("@")twitterSite"> 

1 Comment

And how do i use this on a huge text with several paragraphs? I have tried with ` instead of " but the result is questionable. No errors on blank rows but error on ` character.
44

Use <text></text> or the easier way @:.

3 Comments

It's odd the (at sign colon) @: character sequence syntax doesn't work for me, as I try to upgrade my ASP.NET MVC 3 project to MVC4. The exception I get is: "":" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid."
@Michael That's weird. They must have removed it in version 4.
Hi Kasper Skov, I found the @: issue was not actually related to @: at all. It was related to unnecessarily using @Model, in a @if() {} block. The fix was to drop the at sign on Model. I need to avoid using @ when already in server scope. stackoverflow.com/questions/12809855/…
30

Instead of HTML entity I prefer the use of @Html.Raw("@").

Comments

13

@@ is the escape character for @ in Razor views as stated in Tobiasopdenbrouw's answer.

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.

1 Comment

I have not found a way for Razor/VB.net and using Prismjs. I tried all above for <a href="@Url.Action("Edit", "Antigen", New With {Key .id = item.AntigenId})" class="btn-xs btn-primary">Edit</a>
9

For the question about @RazorCodePart1 @@ @RazorCodePart2, you need 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.

Comments

9

Based on Terje Solem's answer, the UTF-8 code %40 worked for me. This is the original URL I was trying to reach:

https://unpkg.com/@google/[email protected]/dist/markerclustererplus.min.js 

This is what worked for me in my code:

https://unpkg.com/%40google/[email protected]/dist/markerclustererplus.min.js 

Comments

8

I just had the same problem. I declared a variable putting my text with the @.

@{ var twitterSite = "@MyTwitterSite"; } ... <meta name="twitter:site" content="@twitterSite"> 

1 Comment

This was the only one working for me and being valid for Open Graph debuggers.
6

This works for me:

<meta name="author" content="Alan van Buuren @("@Alan_van_Buuren")"> 

Or you can use:

@@Alan_van_Buuren 

Comments

1

You can use @@ for this purpose. Like var email = firstName + '\@@' + domain;

1 Comment

This repeats multiple answers
1

I tried all the options in previous answers and none worked. This is what worked:

@{ string str = @"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$"; } <td>Email</td> <td> <input type="text" id="txtEmail" required name="email" pattern=@str /> </td> 

I created a string variable, passed all the regular expression pattern code into it, and then used the variable in the HTML content. Razor was cool with it.

Comments

1

Just add a variable in the CSHTML file:

var myVariable = @"@"; 

And add it to your layout:

<span class="my-class"><a href="@myVariale" target="_blank" >link text</a></span> 

Comments

1

I couldn't get any of these to work inside my placeholder attribute, so I used an XML special character.

<input type="text" placeholder="fex: firstname&#64;lastname.com"/> 

See more examples at 16.1 How can I use special characters in XML?

1 Comment

My need was to add @ in meta tag. and this works fine. @@ doesn't work there.
-2

Actually, @ should be used with the Razor syntax keywords or to the variable/model to bind a value.

For example: if a test is assigned with a value, i.e., @ { var test = "ABC" }, then you can get the value by settings as @test anywhere there is a .cshtml page in the HTML part.

Otherwise, simply use as @Html.DisplayName("test").

1 Comment

This does not answer the question.
-2

I think in Razor view, @Html.Raw() is the best solution for all versions, and it always works for me. I have added a working example CDN URL to provide a clear idea.

 @Html.Raw("<script src=\"https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js\"></script>") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.