呈现电子邮件会在非MVC项目中使用RazorEngine 3引发TemplateCompilationException

呈现电子邮件会在非MVC项目中使用RazorEngine 3引发TemplateCompilationException,razor,template-engine,razor-2,Razor,Template Engine,Razor 2,我正在尝试在windows服务主机中呈现电子邮件 我使用coxp分叉的Razorengine3,它支持Razor2。 这对一些emailtemplates很好,但有一个会给我带来问题 @model string <a href="@Model" target="_blank">Click here</a> to enter a new password for your account. 这会引发CompilationException:名称“WriteAttrib

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

我使用coxp分叉的Razorengine3,它支持Razor2。

这对一些emailtemplates很好,但有一个会给我带来问题

@model string

<a href="@Model" target="_blank">Click here</a> to enter a new password for your account.
这会引发CompilationException:名称“WriteAttribute”在当前上下文中不存在。因此,作为模型传入字符串并将其放入href属性会导致问题

@model string

<a href="@Model" target="_blank">Click here</a> to enter a new password for your account.
我可以通过以下方式更改此行使其工作:

@Raw(string.Format("<a href=\"{0}\" target=\"_blank\">Klik hier</a>.", @Model))
但这使得模板很难阅读,也很难传递给营销部门进行进一步的样式设计

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

任何关于如何解决这一问题的建议都是非常好的。分享你的经验也是非常受欢迎的

更新1

似乎调用SetTemplateBaseType可能会有所帮助,但是这个方法已经不存在了,所以我想知道如何绑定templatebasetype

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

我使用Windsor注入模板服务,而不是使用Razor对象。下面是代码的简化部分,说明如何设置基本模板类型

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

我使用Windsor注入模板服务,而不是使用Razor对象。下面是代码的简化部分,说明如何设置基本模板类型

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

基于coxp答案的稍加修改的示例,无需注入:

    private static bool _razorInitialized;

    private static void InitializeRazor()
    {
        if (_razorInitialized) return;
        _razorInitialized = true;
        Razor.SetTemplateService(CreateTemplateService());
    }

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

    public static string ParseTemplate(string name, object model)
    {
        InitializeRazor();

        var appFileName = "~/EmailTemplates/" + name + ".cshtml";
        var template = File.ReadAllText(HttpContext.Current.Server.MapPath(appFileName));
        return RazorEngine.Razor.Parse(template, model);
    }
RazorEngine 3.1.0

基于coxp答案的稍加修改的示例,无需注入:

    private static bool _razorInitialized;

    private static void InitializeRazor()
    {
        if (_razorInitialized) return;
        _razorInitialized = true;
        Razor.SetTemplateService(CreateTemplateService());
    }

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

    public static string ParseTemplate(string name, object model)
    {
        InitializeRazor();

        var appFileName = "~/EmailTemplates/" + name + ".cshtml";
        var template = File.ReadAllText(HttpContext.Current.Server.MapPath(appFileName));
        return RazorEngine.Razor.Parse(template, model);
    }

很好用!非常感谢。非常好用!谢谢。