【问题标题】:How to use RazorEngine to include files?如何使用 RazorEngine 包含文件?
【发布时间】:2017-06-12 15:42:53
【问题描述】:

我正在尝试使用 RazorEngine 在模板中包含其他文件,但我有点卡住了。我已经掌握了基础知识,但我希望能够在我的模板中使用 @Include("somefile.html")

这是我到目前为止得到的:

string tpl = @"@Include(""foo.html"");";

ResolvePathTemplateManager r = new ResolvePathTemplateManager(new string[] { "html" });
var config = new TemplateServiceConfiguration();
config.TemplateManager = r;
var service = RazorEngineService.Create(config);

var a = service.RunCompile(tpl, "name", null, new { test = "TEMPLATE" });

当前工作目录有一个html 目录,其中foo.html 所在的位置,但我收到此错误:

无法解析模板名称

【问题讨论】:

    标签: c# razorengine


    【解决方案1】:

    显然,在解析路径时,您不能使用字符串模板。此代码有效:

    ResolvePathTemplateManager r = new ResolvePathTemplateManager(new string[] { "html" });
    var config = new TemplateServiceConfiguration();
    config.TemplateManager = r;
    var service = RazorEngineService.Create(config);
    
    var a = service.RunCompile("foo.html");
    

    foo.html 我可以使用:

    @Include("otherfile.html");
    

    包含来自同一目录的文件。

    【讨论】:

    • FWIW,我们使用DelegateTemplateManager,它允许您传递一个采用单个string 参数而不是ResolvePathTemplateManager 的函数。在该函数中,您可以做任何您想做的事情来解析模板 - 我们有一个我们搜索的路径数组等。 that 是否适用于字符串模板......
    猜你喜欢
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 2010-11-28
    • 2022-01-23
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多