小编Max*_*est的帖子

RazorEngine 3.7.7 - 编译缓存模板时出错

我试图找出我们最近使用RazorEngine 3.7.5及更高版本的问题(试过3.7.7)

例外:

System.ArgumentException:请将模板管理器设置为模板或添加模板"MySolution.Billing.Templates.Layout.cshtml"!

尝试使用Engine.Razor.Compile方法缓存模板时发生.

public void AddTemplate(string templateName, string source)
{
     Engine.Razor.AddTemplate(templateName, source);
}

public void CacheTemplate(string templateName, Type type)
{
     var templateKey = new NameOnlyTemplateKey(templateName, ResolveType.Layout, null);
     Engine.Razor.Compile(templateKey, type);
}
Run Code Online (Sandbox Code Playgroud)

当使用StructureMap为实例创建包含它的服务时,将调用PreloadTemplates方法.每个模板都存储为嵌入式资源,并在使用RazorEngine编译后立即加载到RazorEngine缓存中,以确保尽可能快地加载所有模板.

private void PreloadTemplates()
{
        var embeddedResources = Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(x => x.StartsWith("MySolution.Billing.Templates")).ToList();
        foreach (var invoiceResource in embeddedResources)
        {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(invoiceResource))
            {
                using (var reader = new StreamReader(stream))
                {
                    var template = reader.ReadToEnd();
                    this._templatingService.AddTemplate(invoiceResource, template);
                }
            }
        }

        this._templatingService.CacheTemplate("MySolution.Billing.Templates.Header.cshtml", typeof(HeaderModel));
        this._templatingService.CacheTemplate("MySolution.Billing.Templates.Layout.cshtml", typeof(LayoutModel));
        this._templatingService.CacheTemplate("MySolution.Billing.Templates.Footer.cshtml", null);
}
Run Code Online (Sandbox Code Playgroud)

RazorEngine配置如下

var …
Run Code Online (Sandbox Code Playgroud)

c# wcf razorengine

6
推荐指数
1
解决办法
2488
查看次数

标签 统计

c# ×1

razorengine ×1

wcf ×1