How to write at-sign inside an attribute in an ASP.NET web page using Razor?

I have an HTML like this:

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

However, this gives an error that the username variable does not exist. Outside of HTML attributes, the at sign can be escaped as @@ , but this does not work inside attributes. What is the correct syntax?

+4
source share
2 answers

In MVC3 (e.g. Razor 1.0) double works, e.g. @@ :

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

But in MVC4 (e.g. Razor 2.0), something has changed and it no longer works (maybe this is an error ...).

So, since you are allowed to have a code block for the attribute, you need to write a code block with @(...) , from where you can return @ :

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

or

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

As far as I know, web pages 2.0 use Razor 2.0.

+4
source

Why not html escape @ -sign?

 <img src="..." alt="Find me on twitter as &#64;username" /> 
+1
source

Source: https://habr.com/ru/post/1437042/


All Articles