SamJolly
SamJolly

Reputation: 6477

How to replace @ in email address with <z>@</z> and not get < used instead

I am using Razor with MVC3 and ASP.NET4.5

I need to display [email protected] in a HTML email.

Due to issues with email clients adding mailto links to email addresses I need to alter them to prevent this happening. One recommendation is to add a non existent tag such as <z>. I have tried:

@(username.Replace("@","<z>@</z>"))

But this outputs:

jim&lt;z&gt;@&lt;/z&gt;bloggs.com

How can I ensure I get the following in the HTML:

jim<z>@</z>bloggs.com 

I suspect some tweak to :

@(username.Replace("@","<z>@</z>"))

is the answer. Thanks in advance.

Upvotes: 0

Views: 39

Answers (1)

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12334

You should use @Html.Raw() method, because string escaping is turned on by default in Razor.

@Html.Raw(username.Replace("@","<z>@</z>"))

Though there will be security considerations for this matter as it's a potentially XSS vulnerability, so you would better escape the initial value of the username at first and after that apply your tag insertion.

Upvotes: 1

Related Questions