diff --git a/Readme.md b/Readme.md index c88cb1e..635f383 100644 --- a/Readme.md +++ b/Readme.md @@ -299,10 +299,10 @@ where the template might look like this: @Model.Name @Model.Company @Model.Address.City @Model.Entered - @{for (int i = 0; i < 10; i++) - { - Response.WriteLine(i + "."); - } + @for (int i = 0; i < 10; i++) + { + Response.WriteLine(i + "."); + } You can also render nested templates from string @RenderTemplate("Child Template rendered from String. Name: @Model.Name",Model) diff --git a/Westwind.RazorHosting.Tests/FileTemplates/HelloWorldWithModelSyntax.cshtml b/Westwind.RazorHosting.Tests/FileTemplates/HelloWorldWithModelSyntax.cshtml index 5f4443b..469177d 100644 --- a/Westwind.RazorHosting.Tests/FileTemplates/HelloWorldWithModelSyntax.cshtml +++ b/Westwind.RazorHosting.Tests/FileTemplates/HelloWorldWithModelSyntax.cshtml @@ -6,12 +6,12 @@ - - @Model.Name @Model.Company @Model.Address.City @Model.Entered - - @{for (int i = 0; i < 10; i++) - { - Response.WriteLine(i + "."); - } + + @Model.Name @Model.Company @Model.Address.City @Model.Entered + + @for (int i = 0; i < 10; i++) + { + Response.WriteLine(i + "."); + } \ No newline at end of file diff --git a/Westwind.RazorHosting.Tests/FileTemplates/TestPartial.cshtml b/Westwind.RazorHosting.Tests/FileTemplates/TestPartial.cshtml index afdde5c..2137415 100644 --- a/Westwind.RazorHosting.Tests/FileTemplates/TestPartial.cshtml +++ b/Westwind.RazorHosting.Tests/FileTemplates/TestPartial.cshtml @@ -10,9 +10,9 @@ @Model.Name @Model.Company @Model.Address.City @Model.Entered - @{for (int i = 0; i < 10; i++) - { - Response.WriteLine(i + "."); - } - + @for (int i = 0; i < 10; i++) + { + Response.WriteLine(i + "."); + } + \ No newline at end of file diff --git a/Westwind.RazorHosting.Tests/FileTemplates/TestPartialAndLayout.cshtml b/Westwind.RazorHosting.Tests/FileTemplates/TestPartialAndLayout.cshtml index 44aeb62..15f2d90 100644 --- a/Westwind.RazorHosting.Tests/FileTemplates/TestPartialAndLayout.cshtml +++ b/Westwind.RazorHosting.Tests/FileTemplates/TestPartialAndLayout.cshtml @@ -10,12 +10,12 @@ @RenderPartial("~/Header_Partial.cshtml", Model) - @Model.Name @Model.Company @Model.Address.City @Model.Entered +@Model.Name @Model.Company @Model.Address.City @Model.Entered - @{for (int i = 0; i < 10; i++) - { - Response.WriteLine(i + "."); - } +@for (int i = 0; i < 10; i++) +{ + Response.WriteLine(i + "."); +}
Done
diff --git a/Westwind.RazorHosting/HostContainers/RazorBaseHostContainer.cs b/Westwind.RazorHosting/HostContainers/RazorBaseHostContainer.cs index 265c848..02ebaf4 100644 --- a/Westwind.RazorHosting/HostContainers/RazorBaseHostContainer.cs +++ b/Westwind.RazorHosting/HostContainers/RazorBaseHostContainer.cs @@ -361,7 +361,6 @@ protected virtual void SetError(string message) return; } - LastException = new RazorHostContainerException(message, Utilities.GetTextWithLineNumbers(Engine?.LastGeneratedCode), Engine?.LastException, diff --git a/Westwind.RazorHosting/HostContainers/RazorFolderHostContainer.cs b/Westwind.RazorHosting/HostContainers/RazorFolderHostContainer.cs index 5f08bcf..6b57523 100644 --- a/Westwind.RazorHosting/HostContainers/RazorFolderHostContainer.cs +++ b/Westwind.RazorHosting/HostContainers/RazorFolderHostContainer.cs @@ -120,7 +120,7 @@ public string RenderTemplate(string relativePath, object model = null, TextWrite // Set configuration data that is to be passed to the template (any object) Engine.TemplatePerRequestConfigurationData = new RazorFolderHostTemplateConfiguration() { - TemplatePath = Path.Combine(TemplatePath, relativePath.Replace("~/","").Replace("~","")), + TemplatePath = Path.Combine(TemplatePath, relativePath.Replace("~/", "").Replace("~", "")), TemplateRelativePath = relativePath, LayoutPage = null, IsLayoutPage = isLayoutPage @@ -135,33 +135,35 @@ public string RenderTemplate(string relativePath, object model = null, TextWrite result = Engine.RenderTemplateFromAssembly(item.AssemblyId, model, writer); var config = Engine.TemplatePerRequestConfigurationData as RazorFolderHostTemplateConfiguration; - + if (result == null) { - LastException = new RazorHostContainerException($"Failed to render Page {relativePath}: " + - Engine.ErrorMessage, + SetErrorException(new RazorHostContainerException($"Failed to render Page {relativePath}: " + + Engine.ErrorMessage, Engine.LastGeneratedCode, Engine.LastException, config.TemplatePath, - config); + config)); + return null; } - if (config != null && !config.IsLayoutPage && !string.IsNullOrEmpty(config.LayoutPage)) + if (config != null && !config.IsLayoutPage && !string.IsNullOrEmpty(config.LayoutPage)) { - string layoutTemplate = config.LayoutPage; + string layoutTemplate = config.LayoutPage; config.IsLayoutPage = false; - - string layout = RenderTemplate(layoutTemplate, model, writer,isLayoutPage: true); + + string layout = RenderTemplate(layoutTemplate, model, writer, isLayoutPage: true); if (layout == null) { - LastException = new RazorHostContainerException($"Failed to render Layout Page {layoutTemplate}: " + - Engine.ErrorMessage, + SetErrorException(new RazorHostContainerException($"Failed to render Layout Page {layoutTemplate}: " + + Engine.ErrorMessage, Engine.LastGeneratedCode, - Engine.LastException, - Path.Combine(TemplatePath,layoutTemplate), - config); + Engine.LastException, + Path.Combine(TemplatePath, layoutTemplate), + config)); + result = null; } else @@ -172,10 +174,10 @@ public string RenderTemplate(string relativePath, object model = null, TextWrite } catch (Exception ex) { - SetError(ex.Message); + this.SetError(ex.Message); } finally - { + { writer?.Close(); // reset per request cache @@ -222,7 +224,7 @@ protected virtual CompiledAssemblyItem GetAssemblyFromFileAndCache(string relati var path = relativePath.Replace("/", "\\").Replace("~\\", ""); - string filename = Path.Combine(TemplatePath,path).ToLower(); + string filename = Path.Combine(TemplatePath, path).ToLower(); int fileNameHash = filename.GetHashCode(); if (!File.Exists(filename)) { @@ -230,7 +232,7 @@ protected virtual CompiledAssemblyItem GetAssemblyFromFileAndCache(string relati null, null, filename, - null)); + null)); return null; } @@ -266,7 +268,7 @@ protected virtual CompiledAssemblyItem GetAssemblyFromFileAndCache(string relati null, null, filename, - null)); + null)); return null; } assemblyId = Engine.CompileTemplate(template); @@ -364,6 +366,6 @@ public class CompiledAssemblyItem public string SafeClassName { get; set; } public string Namespace { get; set; } - public string LayoutPage { get; set; } + public string LayoutPage { get; set; } } }