diff --git a/zero/Rendering/RazorRenderer.cs b/zero/Rendering/RazorRenderer.cs index 98b08fa1..96ba2fab 100644 --- a/zero/Rendering/RazorRenderer.cs +++ b/zero/Rendering/RazorRenderer.cs @@ -154,25 +154,27 @@ public class RazorRenderer : IRazorRenderer, IDisposable /// /// Renders a razor view to a string /// - public async Task ViewAsync(string view, object model = null) + public async Task ViewAsync(string view, object model = null, Action onBeforeRender = null) { - return await ViewAsync(BuildActionContext(), view, model); + return await ViewAsync(BuildActionContext(), view, model, onBeforeRender); } /// /// Renders a razor view to a string /// - public async Task ViewAsync(ActionContext context, string view, object model = null) + public async Task ViewAsync(ActionContext context, string view, object model = null, Action onBeforeRender = null) { IView viewResult = FindView(context, view); - using StringWriter stringWriter = new(); + await using StringWriter stringWriter = new(); ViewContext viewContext = BuildViewContext(context, stringWriter, viewResult); viewContext.RouteData = context.RouteData; viewContext.ViewData.Model = model; + onBeforeRender?.Invoke(viewContext); + await viewResult.RenderAsync(viewContext); await stringWriter.FlushAsync(); @@ -336,10 +338,10 @@ public interface IRazorRenderer /// /// Renders a razor view to a string /// - Task ViewAsync(string view, object model = null); + Task ViewAsync(string view, object model = null, Action onBeforeRender = null); /// /// Renders a razor view to a string /// - Task ViewAsync(ActionContext context, string view, object model = null); + Task ViewAsync(ActionContext context, string view, object model = null, Action onBeforeRender = null); } \ No newline at end of file