private ITemplateKey GetKeyAndAdd(string template, string name = null) { if (name == null) { name = "dynamic_" + Guid.NewGuid().ToString(); } var key = _service.GetKey(name); var source = new LoadedTemplateSource(template); _service.Configuration.TemplateManager.AddDynamic(key, source); return(key); }
public void RazorEngineService_GetInformativeRuntimeErrorMessage() { RunTestHelper(service => { const string template = "@foreach (var i in Model.Unknown) { @i }"; string file = Path.GetTempFileName(); try { File.WriteAllText(file, template); var source = new LoadedTemplateSource(template, file); var exn = Assert.Throws<Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(() => { string result = service.RunCompile(source, "test", null, new object()); }); // We now have a reference to our template in the stacktrace var stack = exn.StackTrace.ToLowerInvariant(); var expected = file.ToLowerInvariant(); Assert.IsTrue( stack.Contains(expected), "Could not find reference to template (" + expected + ") in stacktrace: \n" + stack); } finally { File.Delete(file); } }); }