Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor ViewEngine: How do I escape the "@" symbol?

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.

like image 517
Aaronontheweb Avatar asked Mar 13 '11 05:03

Aaronontheweb


People also ask

How do you escape the razor symbol?

In Razor, `@` symbol is used to transition from HTML to C#. To escape an '@' symbol in razor markup, use two '@' symbols.

What is directive in razor?

Razor directives are represented by implicit expressions with reserved keywords following the @ symbol. A directive typically changes the way a view is parsed or enables different functionality. Understanding how Razor generates code for a view makes it easier to understand how directives work. CSHTML Copy.

What is HTML Razor view?

Razor is a standard markup syntax that allows us to embed server code into the web pages. It uses its own syntax and keywords to generate view. If there is server code in the web page, server executes that code first then send response to the browser. It allows us to perform logical tasks in the view page.

What are razor expressions used for?

Razor provides expression encoding to avoid malicious code and security risks. In case, if user enters a malicious script as input, razor engine encode the script and render as HTML output.


2 Answers

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.

like image 71
JasCav Avatar answered Sep 28 '22 05:09

JasCav


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")> 
like image 31
Matt Avatar answered Sep 28 '22 05:09

Matt