在非 MVC 项目中使用 RazorEngine 3 渲染电子邮件会引发 TemplateCompilationException [英] Rendering an email throws a TemplateCompilationException using RazorEngine 3 in a non-MVC project

查看:34
本文介绍了在非 MVC 项目中使用 RazorEngine 3 渲染电子邮件会引发 TemplateCompilationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 服务主机中呈现电子邮件.

I am trying to render emails in a windows service host.

我使用由 coxp 分叉的 RazorEngine 3,它支持 Razor 2.https://github.com/coxp/RazorEngine/tree/release-3.0/src

I use RazorEngine 3 forked by coxp which has support for Razor 2. https://github.com/coxp/RazorEngine/tree/release-3.0/src

这适用于几个电子邮件模板,但有一个给我带来了问题.

This works fine for a couple of emailtemplates but there is one causing me problems.

@model string

<a href="@Model" target="_blank">Click here</a> to enter a new password for your account.

这会引发 CompilationException:名称WriteAttribute"在当前上下文中不存在.因此,将字符串作为模型传入并将其放入 href 属性会导致问题.

This throws a CompilationException: The name 'WriteAttribute' does not exist in the current context. So passing in a string as model and putting it in the href-attribute causes problems.

我可以通过以下方式更改此行使其工作:

I can make it work by changing this line by:

@Raw(string.Format("<a href=\"{0}\" target=\"_blank\">Klik hier</a>.", @Model))

但这使得模板非常不可读,并且更难传递给营销部门以进行进一步的样式设置.

but this makes the template very unreadable and harder to pass along to a marketing department for further styling.

我想补充一点,通过使用 Nuget 包来引用 RazorEngine 不是解决方案,因为它基于 Razor 1,并且在此过程中的某个地方,system.web.razor 的 DLL 被版本 2 替换,这会破坏任何代码使用剃刀引擎.使用 Razor 2 从新功能中受益并保持最新状态似乎更有趣.

I like to add that referencing the RazorEngine by using a Nuget package is not a solution since it is based on Razor 1 and somewhere along the process the DLL for system.web.razor gets replaced by version 2 which breaks any code using RazorEngine. It seems more interesting to use Razor 2 to benefit from the new features and to be up to date.

有关如何解决此问题的任何建议都会很棒.也非常欢迎分享您的经验.

Any suggestions on how to fix this would be great. Sharing your experiences is also very welcome.

更新 1

好像调用 SetTemplateBaseType 可能有帮助,但是这个方法已经不存在了,所以我想知道如何能够绑定模板基类型?

It seems like calling SetTemplateBaseType might help, but this method does not exist anymore, so I wonder how to be able to bind the templatebasetype?

//Missing method in the new RazorEngine build from coxp.
Razor.SetTemplateBaseType(typeof(HtmlTemplateBase<>));

推荐答案

我使用 Windsor 来注入模板服务,而不是使用 Razor 对象.这是代码的简化部分,展示了如何设置基本模板类型.

I use Windsor to inject the template service rather than using the Razor object. Here is a simplified part of the code that shows how to set the base template type.

    private static ITemplateService CreateTemplateService()
    {
        var config = new TemplateServiceConfiguration
                         {
                             BaseTemplateType = typeof (HtmlTemplateBase<>),
                         };
        return new TemplateService(config);
    }

这篇关于在非 MVC 项目中使用 RazorEngine 3 渲染电子邮件会引发 TemplateCompilationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆