[asp.net-mvc-3] Escape @ character in razor view engine

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?

This question is related to asp.net-mvc-3 razor escaping

The answer is


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


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


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>


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.


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


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


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

@{
   var twitterSite = "@MyTwitterSite";
}

...

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

this work for me

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

Or yoy can use: @@Alan_van_Buuren

:D


I know this question is old, but I tried all of the above and it didn't help me escape the character "@" in ASP.NET full framework (MVC 5) inside a URL. Based on Terje Solem's answer though, 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

and with with the code in it:

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

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

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.


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


Razor @ escape char to symbols...

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

or

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

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")


Examples related to asp.net-mvc-3

Better solution without exluding fields from Binding IIs Error: Application Codebehind=“Global.asax.cs” Inherits=“nadeem.MvcApplication” Can we pass model as a parameter in RedirectToAction? return error message with actionResult Why is this error, 'Sequence contains no elements', happening? Failed to load resource: the server responded with a status of 500 (Internal Server Error) in Bind function 500.19 - Internal Server Error - The requested page cannot be accessed because the related configuration data for the page is invalid String MinLength and MaxLength validation don't work (asp.net mvc) How to set the value of a hidden field from a controller in mvc How to set a CheckBox by default Checked in ASP.Net MVC

Examples related to razor

Uncaught SyntaxError: Invalid or unexpected token How to pass a value to razor variable from javascript variable? error CS0103: The name ' ' does not exist in the current context how to set radio button checked in edit mode in MVC razor view @Html.DropDownListFor how to set default value Razor MVC Populating Javascript array with Model Array How to add "required" attribute to mvc razor viewmodel text input editor How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas Multiple radio button groups in MVC 4 Razor How to hide a div element depending on Model value? MVC

Examples related to escaping

Uses for the '&quot;' entity in HTML Javascript - How to show escape characters in a string? How to print a single backslash? How to escape special characters of a string with single backslashes Saving utf-8 texts with json.dumps as UTF8, not as \u escape sequence Properly escape a double quote in CSV How to Git stash pop specific stash in 1.8.3? In Java, should I escape a single quotation mark (') in String (double quoted)? How do I escape a single quote ( ' ) in JavaScript? Which characters need to be escaped when using Bash?