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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
If you are adding Twitter meta tags
and your Twitter username is, say, foobar
it should look like this
<meta name="twitter:site" <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e1edecf6e7ecf6bfc2">[email protected]</a>("@foobar")>
Method 3
if you need to comment @ symbols in css code and just @@ wont work, use this:
@("@@font-face"){ ... css ...}
Method 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 @ would complicate things.
So, my code was <a href="~/path/[email protected]('@')@Model.SomePropery" rel="nofollow noreferrer noopener">
@Html.Raw('@')@Model.SomePropery would have worked as well.
Method 5
If you are adding Schema.org JSON-LD Tags, @Matt’s answer worked for me:
....
@("@context"): "https://schema.org",
....
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0