Issue with RazorEngine, Templates and email addresses

100 Views Asked by At

Hoping someone can help with this. It's causing me a real headache.

RazorEngine is throwing an odd error whenever one of my templates contains an email address that contains a hyphen at the end of the user part of the address (e.g. [email protected]). The error is long, and has great explanations about why dynamic models don't work etc., however, the relevant part is

The name 'domain' does not exist in the current context.

I don't get this with the same template when I change the email address to one that does not have the same -@ structure, so it's definitely something to do with how RazorEngine is parsing that. It seems that particular sequence leads RazorEngine to read the @ as the start of a razor command.

I've tried replacing the '-' with the HTML entity - but the result is the same. Has anyone else seen this or does anyone know of any other workarounds I could try?

1

There are 1 best solutions below

2
On BEST ANSWER

The issue is not JUST the hyphen, the issue is the -@ symbol. It is translating that as if you were passing the variable "domain" in your model, can't find it and freaks out. You will need to escape it which there are several ways of doing so, perhaps if you show your code then we could suggest which route to take. Most commonly this is solved by using something like @Html.Raw("[email protected]")

I can find comments and documentation everywhere on things like using underscores for html data attributes (ie data-test should be written in razor as data_test) but for the life of me I have never been able to find any documentation anywhere on why the hyphen is treated the way it is.