110

I'm trying to output some Twitter handles in ASP.NET MVC3 in conjunction with the Twitter @Anywhere API, and I haven't been able to figure out how to actually escape the "@" symbol in a Razor view.

Does anyone know what the syntax is for escaping the "@" character in Razor?

I've tried using <text></text> and that results in a JIT error.

1
  • 2
    You can also use the HTML entity &#64;.
    – rsenna
    Feb 24, 2014 at 18:00

5 Answers 5

162

You have to use @@ to escape the @ symbol.

One important thing to notice is that you DO NOT need to escape the @ symbol when it exists within an email address. Razor should be smart enough to figure that out on its own.

2
  • 4
    <a href="" title="this @@ won't work"></a>, both syntax error in VS and parser error in runtime.
    – Tien Do
    Jul 25, 2012 at 1:35
  • 2
    @Tien see the last HTML character code here worked for me: stackoverflow.com/a/13584640/84206
    – AaronLS
    Dec 27, 2012 at 20:53
32

If you are adding Twitter meta tags

and your Twitter username is, say, foobar

it should look like this

<meta name="twitter:site" content=@("@foobar")>
0
20

if you need to comment @ symbols in css code and just @@ wont work, use this:

@("@@font-face"){ ... css ...}
1
  • 1
    This is the best method as the HTML entity isn't helpful inside CSS.
    – ChrisF
    Jan 30, 2014 at 21:06
4

I had yet another odd case: pass '@' + @Model.SomeProperty to a link href.

The best solution for this case was given IMO in this answer to a similar question. Both @@ and @: didn't work while using the html code &#64; would complicate things.

So, my code was <a href="~/path/?query=@('@')@Model.SomePropery">

@Html.Raw('@')@Model.SomePropery would have worked as well.

0

If you are adding Schema.org JSON-LD Tags, @Matt's answer worked for me:

  ....
  @("@context"): "https://schema.org",
  ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.