Restore the TextWriter to ViewContext

This commit is contained in:
Ryan Nowak 2014-03-20 16:40:09 -07:00
parent 8ea196023e
commit 6610788b75
3 changed files with 19 additions and 1 deletions

View File

@ -36,7 +36,9 @@ namespace Microsoft.AspNet.Mvc
var viewContext = new ViewContext(_serviceProvider, context.HttpContext, context.RouteValues, ViewData) var viewContext = new ViewContext(_serviceProvider, context.HttpContext, context.RouteValues, ViewData)
{ {
Url = new UrlHelper(context.HttpContext, context.Router, context.RouteValues), Url = new UrlHelper(context.HttpContext, context.Router, context.RouteValues),
Writer = writer,
}; };
await view.RenderAsync(viewContext, writer); await view.RenderAsync(viewContext, writer);
} }
} }

View File

@ -31,7 +31,20 @@ namespace Microsoft.AspNet.Mvc.Razor
using (var bodyWriter = new StringWriter(contentBuilder)) using (var bodyWriter = new StringWriter(contentBuilder))
{ {
Output = bodyWriter; 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(); var bodyContent = contentBuilder.ToString();

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Mvc.Rendering namespace Microsoft.AspNet.Mvc.Rendering
@ -23,5 +24,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
public ViewData ViewData { get; private set; } public ViewData ViewData { get; private set; }
public IDictionary<string, object> ViewEngineContext { get; private set; } public IDictionary<string, object> ViewEngineContext { get; private set; }
public TextWriter Writer { get; set; }
} }
} }