diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs index 2f14de3cc3..b083efb127 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs @@ -36,7 +36,9 @@ namespace Microsoft.AspNet.Mvc var viewContext = new ViewContext(_serviceProvider, context.HttpContext, context.RouteValues, ViewData) { Url = new UrlHelper(context.HttpContext, context.Router, context.RouteValues), + Writer = writer, }; + await view.RenderAsync(viewContext, writer); } } diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs index 87a9c6a4cf..7d639f5a6a 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs @@ -31,7 +31,20 @@ namespace Microsoft.AspNet.Mvc.Razor using (var bodyWriter = new StringWriter(contentBuilder)) { Output = bodyWriter; - await ExecuteAsync(); + + // The writer for the body is passed through the ViewContext, allowing things like HtmlHelpers + // and ViewComponents to reference it. + var oldWriter = context.Writer; + context.Writer = bodyWriter; + + try + { + await ExecuteAsync(); + } + finally + { + context.Writer = oldWriter; + } } var bodyContent = contentBuilder.ToString(); diff --git a/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs index d1d966d63f..5f3ef730e3 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewContext.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using Microsoft.AspNet.Abstractions; namespace Microsoft.AspNet.Mvc.Rendering @@ -23,5 +24,7 @@ namespace Microsoft.AspNet.Mvc.Rendering public ViewData ViewData { get; private set; } public IDictionary ViewEngineContext { get; private set; } + + public TextWriter Writer { get; set; } } }