将模型数据绑定到 razor .cshtml 文件以通过电子邮件发送

Bind model data to a razor .cshtml file to send via email

我有一个 .cshtml 文件,作为 html 电子邮件的订单确认。

现在我想阅读这个 .cshtml 文件并将某个视图模型绑定到 .cshtml 文件。

最后我想收到一个格式化的 html 字符串以传递到我的电子邮件正文。

即使它需要第 3 方开源,我怎样才能优雅地做到这一点。

您可以使用 RazorEngine NuGet,它允许您将 Razor 模板呈现为字符串:

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