Search code examples
c#razorengine

How to use RazorEngine to include files?


I am trying to use RazorEngine to include other files in a template, but I am a little stuck. I got the basics working, but I want to be able to use @Include("somefile.html") in my template.

This is what I got until now:

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

The current working directory has a html dir where foo.html is located, yet I get this error:

Could not resolve template name


Solution

  • Apperently, when resolving paths, you cannot use a string-template. This code works:

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

    and in foo.html I can use:

    @Include("otherfile.html");
    

    to include a file from the same dir.