public TemplateServiceConfiguration() { Activator = new DefaultActivator(); CompilerServiceFactory = #if RAZOR4 new Roslyn.RoslynCompilerServiceFactory(); #else new DefaultCompilerServiceFactory(); #endif EncodedStringFactory = new HtmlEncodedStringFactory(); #if !RAZOR4 #pragma warning disable 0618 // Backwards Compat. CodeInspectors = new List <ICodeInspector>(); #pragma warning restore 0618 // Backwards Compat. #endif ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); CachingProvider = new DefaultCachingProvider(); TemplateManager = new DelegateTemplateManager(); Namespaces = new HashSet <string> { "System", "System.Collections.Generic", "System.Linq" }; var config = RazorEngineConfigurationSection.GetConfiguration(); Language = (config == null) ? Language.CSharp : config.DefaultLanguage; }
public void RazorEngineService_TestInvalidatingCachingProvider() { var cache = new InvalidatingCachingProvider(); var mgr = new DelegateTemplateManager(); var template = @"@if (Model.MyPrice == 0) { <text>free</text> } else { <text>@Model.MyPrice</text> }"; var template2 = @"@if (Model.MyPrice == 0) { <text>totally free</text> } else { <text>@Model.MyPrice</text> }"; RunTestHelper(service => { var model = new { MyPrice = 0 }; string result = service.RunCompile(template, "key", null, model); Assert.AreEqual("free", result.Trim()); cache.InvalidateCache(service.GetKey("key")); mgr.RemoveDynamic(service.GetKey("key")); string result2 = service.RunCompile(template2, "key", null, model); Assert.AreEqual("totally free", result2.Trim()); }, config => { config.CachingProvider = cache; config.TemplateManager = mgr; }); }
/// <summary> /// Default configuration constructor /// </summary> /// <param name="emailsTemplatesFolder"></param> public RazorEngineEmailTemplateConfiguration(IEmailTemplateConfiguration basEmailTemplateConfiguration) { if (string.IsNullOrEmpty(basEmailTemplateConfiguration.TemplateFolder)) { var directoryInfo = new FileInfo(Assembly.GetEntryAssembly().Location).Directory; if (directoryInfo != null) { TemplateFolder = directoryInfo.FullName; } } else { TemplateFolder = basEmailTemplateConfiguration.TemplateFolder; } if (!Directory.Exists(TemplateFolder)) { throw new DirectoryNotFoundException($"Template directory {TemplateFolder} doesn't exists."); } TemplateManager = new DelegateTemplateManager(name => { var templatePath = Path.Combine(TemplateFolder, name); using (var reader = new StreamReader(templatePath)) { return(reader.ReadToEnd()); } }); }
/// <summary> /// Default configuration constructor /// </summary> /// <param name="emailsTemplatesFolder"></param> public EmailTemplateConfiguration(string emailsTemplatesFolder) { if (string.IsNullOrEmpty(emailsTemplatesFolder)) { var directoryInfo = new FileInfo(Assembly.GetEntryAssembly().Location).Directory; if (directoryInfo != null) { TemplateFolder = directoryInfo.FullName; } } else { TemplateFolder = emailsTemplatesFolder.StartsWith("~/") ? (HttpContext.Current != null ? HttpContext.Current.Server.MapPath(emailsTemplatesFolder) : Path.Combine(Assembly.GetEntryAssembly().Location, emailsTemplatesFolder.TrimStart('~', '/'))) : emailsTemplatesFolder; } if (!Directory.Exists(TemplateFolder)) { throw new DirectoryNotFoundException($"Template directory {TemplateFolder} doesn't exists."); } TemplateManager = new DelegateTemplateManager(name => { var templatePath = Path.Combine(TemplateFolder, name); using (var reader = new StreamReader(templatePath)) { return(reader.ReadToEnd()); } }); }
public RazorTemplatePaser(IParserConfig parseConfig) { config = new TemplateServiceConfiguration(); config.Debug = parseConfig.EnableDebug; //config.TemporaryDirectory config.Language = Language.CSharp; config.DisableTempFileLocking = true;//as well. This will work in any AppDomain (including the default one) if (config.Debug) { config.DisableTempFileLocking = false; } //config.CachingProvider = new DefaultCachingProvider(t => { }); config.CachingProvider = new DefaultCachingProvider(); config.EncodedStringFactory = new RazorEngine.Text.RawStringFactory(); // Raw string encoding. config.ReferenceResolver = new LocalReferenceResolver(); //DelegateTemplateManager: (default) Used as the default for historical reasons, easy solution when using dynamic template razor strings. //ResolvePathTemplateManager: Used to resolve templates from a given list of directory paths. Doesn't support adding templates dynamically via string. You can use a full path instead of a template name. //EmbeddedResourceTemplateManager: Used to resolve templates from assembly embedded resources. Uses Assembly.GetManifestResourceStream(Type, string) to load the template based on the type provided. //WatchingResolvePathTemplateManager: Same as ResolvePathTemplateManager but watches the filesystem and invalidates the cache. Note that this introduces a memory leak to your application, so only use this is you have an AppDomain recycle strategy in place or for debugging purposes. templateManager = new DelegateTemplateManager(); config.TemplateManager = templateManager; //config.EncodedStringFactory = new RazorEngine.Text.HtmlEncodedStringFactory(); // Html encoding. if (parseConfig.EnableSandbox) { var service = IsolatedRazorEngineService.Create(new SandBoxConfigCreator(config), SandBox.SandboxCreator); Engine.Razor = service; } else { var service = RazorEngineService.Create(config); Engine.Razor = service; } }
public TemplateServiceConfiguration() { Activator = new DefaultActivator(); CompilerServiceFactory = new DefaultCompilerServiceFactory(); EncodedStringFactory = new HtmlEncodedStringFactory(); #if !RAZOR4 #pragma warning disable 0618 // Backwards Compat. CodeInspectors = new List<ICodeInspector>(); #pragma warning restore 0618 // Backwards Compat. #endif ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); CachingProvider = new DefaultCachingProvider(); TemplateManager = new DelegateTemplateManager(); Namespaces = new HashSet<string> { "System", "System.Collections.Generic", "System.Linq" }; var config = RazorEngineConfigurationSection.GetConfiguration(); Language = (config == null) ? Language.CSharp : config.DefaultLanguage; }
public TemplateServiceConfiguration() { Activator = new DefaultActivator(); CompilerServiceFactory = new Roslyn.RoslynCompilerServiceFactory(); EncodedStringFactory = new HtmlEncodedStringFactory(); ReferenceResolver = new UseCurrentAssembliesReferenceResolver(); CachingProvider = new DefaultCachingProvider(); TemplateManager = new DelegateTemplateManager(); Namespaces = new HashSet <string> { "System", "System.Collections.Generic", "System.Linq" }; Language = Language.CSharp; }