Fluent电子邮件错误,无法编译模板

问题描述 投票:0回答:2

我将我的mvc 3.0项目更新为.net 4.0和mvc 4.0,并发现Fluent电子邮件停止工作。我正在尝试使用最新版本http://www.nuget.org/packages/fluent-email/1.2.2和预发行版本http://www.nuget.org/packages/fluent-email/1.3.0-RazorEngine

但是出现错误Unable to compile template

An exception of type 'RazorEngine.Templating.TemplateCompilationException' occurred in RazorEngine.dll but was not handled in user code

Additional information: Unable to compile template. Source file 'C:\Windows\TEMP\xrhyjmc5.0.cs' could not be found

也许有人知道该如何解决?

我的电话代码:

                    email = Email
                    .From(model.FromEmail, model.Username)
                    .To(betsTipsterEmail)
                    .Subject(emailSubject)
                    .UsingTemplateFromFile(emailBodyFile, model)
                    .Body(model.Body)
                    .UsingClient(client);

我的Confirmation.html模板:

<html>
    <body>
        <p>
            Hi @Model.Username,

            bla<br /><br />
            bla<br /><br />
            <a href="@Model.Url">@Model.Url</a><br /><br />
            bla
        </p>
        <p>
            Kind Regards,<br /><br />
            bla
        </p>
    </body>
</html> 

RazorEngine版本3.2.0正在查看本文http://www.britishdeveloper.co.uk/2011/07/razorengine-templatecompilationexceptio.html,但没有帮助

c# email asp.net-mvc-4 .net-4.5
2个回答
0
投票

我遇到了类似的问题。尝试将每个@Model调用都用大括号括起来。例如。 @(Model.Username)。 1.3.0的最新稳定版本刚刚发布,您也应该更新。


0
投票

对于那些在.Net Core中有此问题的人。我将EmbeddedResources用作电子邮件模板,并使用DI添加剃刀渲染,如下所示:services.AddRazorRenderer()在检查了FluentEMail Razor和RazorLight doc的单元测试之后我发现我应该将typeof(rootType)传递给services.AddRazorRenderer(typeof(rootType)),以使我的代码正常工作,而不这样做,RazorLight无法找到TemplateKey。

来自FluentEmail Razor测试的示例波纹管展示了如何添加RazorRender:

public void Should_be_able_to_use_project_layout_with_viewbag()
        {
            var projectRoot = Directory.GetCurrentDirectory();
            Email.DefaultRenderer = new RazorRenderer(projectRoot);

            string template = @"
@{
    Layout = ""./Shared/_Layout.cshtml"";
}
sup @Model.Name here is a list @foreach(var i in Model.Numbers) { @i }";

            dynamic viewBag = new ExpandoObject();
            viewBag.Title = "Hello!";
            var email = new Email(fromEmail)
                .To(toEmail)
                .Subject(subject)
                .UsingTemplate(template, new ViewModelWithViewBag{ Name = "LUKE", Numbers = new[] { "1", "2", "3" }, ViewBag = viewBag});

            Assert.AreEqual($"<h1>Hello!</h1>{Environment.NewLine}<div>{Environment.NewLine}sup LUKE here is a list 123</div>", email.Data.Body);
        }

FluentEmail提供了三种添加RazorRenderer的方法,您可以检查FluentEmailRazorBuilderExtensions.cs,选择正确的一种。

如果您遇到相同的问题,请参阅FluentEmail Razor TestRazorLight DocFluentEmail issue

© www.soinside.com 2019 - 2024. All rights reserved.