如何使用RazorEngine包含文件?

时间:2017-06-12 15:42:53

标签: c# razorengine

我正在尝试使用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位于apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.0" defaultConfig { applicationId "com.example.myfirstapp" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' } ,但我收到此错误:

  

无法解析模板名称

1 个答案:

答案 0 :(得分: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");

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