Ser*_*gii 20 .net razor razorengine
有人可以解释它们之间的区别,利弊
我需要选择一个用于生成电子邮件.这些要求很常见:快速,易用.似乎所有这些都具有我需要的所有功能但是因为我是Razor新手,我不太清楚哪一个更好.
谢谢.
vor*_*olf 20
我自己尝试了所有3个库,发现了一些差异.
dynamic类型.可以使用预编译模板.至于我,我选择了RazorEngine.这里还有代码如何使用这些库:
RazorEngine
string html = Razor.Parse(templateContent, model, templatePath);
Run Code Online (Sandbox Code Playgroud)
RazorTemplates
if (!_templatesCache.ContainsKey(templatePath))
{
var compiledTemplate = Template.Compile(templateContent);
_templatesCache.Add(templatePath, compiledTemplate);
}
string html = _templatesCache[templatePath].Render(model);
Run Code Online (Sandbox Code Playgroud)
RazorMachine
private readonly Lazy<RazorMachine> _lazyRazorMachine =
new Lazy<RazorMachine>(() => new RazorMachine());
//...
var rm = _lazyRazorMachine.Value;
string html = rm.ExecuteContent(templateContent, model, null, true).Result;
Run Code Online (Sandbox Code Playgroud)
还有一些性能测试,在同一个模板上对每个库进行了2次测试,所有这些都具有相似的性能,没有太大区别:
RazorEngine - 1731 ms,0.1 ms
RazorTemplates - 1753 ms,0.1 ms
RazorMachine - 1608 ms,0.1 ms