Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to RunComplie on updated template using same key in RazorEngine?

string template = "Hello @Model.Name, welcome to RazorEngine!";
var result = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });

Now i update my existing template to as below. I get my template from database.

string template = "Hello @Model.Name, welcome to My World!";

Whenever i do that i get an error The same key was already used for another template.

What is the best way to fix this issue?

like image 241
dev Avatar asked Dec 24 '22 20:12

dev


1 Answers

The issue is that you are not using a template key that is unique to the template code you are passing in. RazorEngine caches the templates and compiles them so the next time round it's faster to run.

var helloTemplate = "Hello @Model.Name";

string result;
var model = new { Name = "World" };

//Has the template already been used? If so, Run without compilation is faster
if(Engine.Razor.IsTemplateCached("helloTemplate", null))
{
    result = Engine.Razor.Run("helloTemplate", null, model);
}
else
{
    result = Engine.Razor.RunCompile(helloTemplate, "helloTemplate", null, model);
}
like image 143
DavidG Avatar answered Dec 28 '22 06:12

DavidG