2017-06-12 58 views
2

我想使用RazorEngine在模板中包含其他文件,但我有点卡住了。我获得了基本工作,但我希望能够在我的模板中使用@Include("somefile.html")如何使用RazorEngine包含文件?

这是我到现在为止:

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 dir其中foo.html所在,但我得到这个错误:

Could not resolve template name

回答

1

Apperently,解决路径时, ,你不能使用字符串模板。此代码的工作:

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"); 

包括来自同一目录中的文件。

+1

FWIW,我们使用'DelegateTemplateManager',它允许你传递一个函数,它只需要一个'string'参数而不是'ResolvePathTemplateManager'。在那个函数中,你可以做任何你想要解析模板的东西 - 我们有一个我们搜索的路径数组等。无论_that_是否适用于字符串模板... –