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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With