I'm using RazorEngine v3.3 to create emails using template files (emails are sent using SendGrid Web API). I implemented a base template so I can use my own html helpers by overriding the WriteTo() method as shown here .
My problem is that my emails are part Html and part Text. For the Html templates, I use razor's default implementation that html-encodes the @Model values. This is because some of the data comes from user input. However, I can't use the same implementation for the text part as Html will not be interpreted when being read.
So the way I see it I have 3 options:
- use @Raw(Model) in all my text based templates to ignore Html encoding
- create an other base template for my text templates which doesn't encode to Html
- modify my Html base template so the WriteTo() method doesn't encode anything
The 1st solution seems the safest, but I have about 50 text based templates to go through, and it reduces readability.
The 2nd solution seems the cleanest to me, however this would prevent the use of cache, as I would be constantly doing Razor.SetTemplateService()
to reassign the right base template ?
What would you recommend doing ? Thanks