-
Notifications
You must be signed in to change notification settings - Fork 92
Switch from RazorEngine cshtml templates
Alexander edited this page Apr 1, 2020
·
7 revisions
if (Engine.Razor.IsTemplateCached(TemplateName, typeof(BaseEmailModel)))
{
parsedContent = Engine.Razor.Run(TemplateName, typeof(BaseEmailModel), model);
}
else
{
var viewPath = Path.Combine(Statics.EmailTemplatesPath, $"{TemplateName}.cshtml");
var viewContent = ReadTemplateContent(viewPath);
parsedContent = Engine.Razor.RunCompile(viewContent, TemplateName, null, model);
}
private static ConcurrentDictionary<string, RazorEngineCompiledTemplate> TemplateCache = new ConcurrentDictionary<string, RazorEngineCompiledTemplate>();
int hashCode = TemplateName.GetHashCode();
RazorEngineCompiledTemplate compiledTemplate = TemplateCache.GetOrAdd(hashCode, i =>
{
RazorEngine razorEngine = new RazorEngine();
string viewPath = Path.Combine(Statics.EmailTemplatesPath, TemplateName + ".cshtml");
return razorEngine.Compile(File.ReadAllText(viewPath));
});
return compiledTemplate.Run(model);
You need to remove @using and @model directives from template as the RazorEngineCore controls them