public class RazorTemplateGenerator
{
public void SetupReportTemplating(string baseDirectory = null)
{
var cfg = new TemplateServiceConfiguration
{
Debug = true
};
Engine.Razor = RazorEngineService.Create(cfg);
var folder = Path.Combine(baseDirectory ?? System.AppDomain.CurrentDomain.BaseDirectory, "Pages","PageTemplates");
var templates = Directory.GetFiles(folder, "*.cshtml");
foreach (var fName in templates)
{
var info = new FileInfo(fName);
var tName = Path.GetFileNameWithoutExtension(info.Name);
var template = File.ReadAllText(fName);
var src = new LoadedTemplateSource(template, fName.Replace(@"\bin",String.Empty));
Engine.Razor.AddTemplate(tName, src);
}
}
public void CheckCompile(string name)
{
Engine.Razor.Compile(name);
}
public string RenderPage(string viewName, object model)
{
return Engine.Razor.RunCompile(viewName, model.GetType(), model);
}
}