Escape @ character in razor view engine
Asked Answered
V

16

677

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 on my cshtml page it gives me parse error

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

How do I escape '@' character?

Vondavonni answered 2/9, 2010 at 10:35 Comment(2)
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.Annecy
@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)Evanescent
L
1098

@@ should do it.

Lemmie answered 2/9, 2010 at 12:2 Comment(7)
This doesn't seem to work in this case: @RazorCodePart1 @@ @RazorCodePart2 e.g. a literal @ between two Razor code snippets.Perigon
The best solution would be using the HTML-encoded character string for the @-character: @Booker
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.Brackely
@WoIIe not in a URL.Shoe
It does not work. I tried all solutions at this page. None of them works for me :/Theurer
In href use @("@")Polonium
I needed the solution from @AsiriDissanayaka for the src in a script tag.Burley
H
194

Razor @ escape char to symbols...

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

or

<img src="..." alt="Find me on twitter as @("@")username" />
Hurleigh answered 29/1, 2013 at 13:19 Comment(1)
This method seems the best as it will also work for @media css stuff, whereas the HTML entity way probably will not.Causerie
F
50

@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">
Faubion answered 25/3, 2016 at 20:46 Comment(1)
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.Panorama
P
43

use <text></text> or the easier way @:

Papyraceous answered 14/7, 2011 at 20:13 Comment(3)
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."Ratafia
@Michael That's weird. They must have removed it in version 4.Papyraceous
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. #12810355Ratafia
B
30

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

Beckiebeckley answered 30/3, 2014 at 16:30 Comment(0)
B
13

@@ 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.

Beneficiary answered 13/9, 2010 at 13:31 Comment(1)
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>Beetner
I
9

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.

Isomorphism answered 7/10, 2011 at 18:37 Comment(0)
M
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">
Message answered 22/3, 2015 at 15:30 Comment(1)
This was the only one working for me and being valid for Open Graph debuggers.Cheryllches
A
8

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
Alix answered 3/3, 2020 at 18:6 Comment(0)
T
5

this work for me

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

Or yoy can use:

@@Alan_van_Buuren
Toniatonic answered 22/1, 2016 at 5:20 Comment(0)
Q
1

I tried all the options above and none worked. This is what I did that 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 varible and passed all the RegEx pattern code into it, then used the variable in the html, and Razor was cool with it.

Quintin answered 27/10, 2015 at 20:24 Comment(0)
I
1

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

Irena answered 24/11, 2016 at 8:36 Comment(1)
This repeats multiple answersGynecic
C
1

just add a variable in CSHTML file

var myVariable = @"@";

and add it to your layout

<span class="my-class"><a href="@myVariale" target="_blank" >link text</a></span>
Croissant answered 6/10, 2017 at 12:24 Comment(0)
R
1

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

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

See more examples here. https://www.dvteclipse.com/documentation/svlinter/How_to_use_special_characters_in_XML.3F.html

Romain answered 27/1, 2018 at 10:56 Comment(1)
My need was to add @ in meta tag. and this works fine. @@ doesn't work there.Polonium
A
-1

Actually @ should be used with the Razor syntax Keywords or to the variable/model to bind a Value.

For Eg: if test is assigned with value i.e @ { var test = "ABC" } then you can get the value by settings as @test anywhere is cshtml page in html part. otherwise, simple use as @Html.DisplayName("test")

Arsine answered 25/6, 2018 at 9:53 Comment(1)
This does not answer the question.Gynecic
D
-1

I think in Razor view @Html.Raw() is the best solution for all version and always works for me. I have added an working example cdn URL to provide clear idea.

 @Html.Raw("<script src=\"https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js\"></script>")
Delladelle answered 9/2, 2023 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.