Make Flush work without requiring curly braces

Fixes #1547
This commit is contained in:
Kirthi Krishnamraju 2014-12-05 11:42:27 -08:00
parent 275d03a958
commit d8455c3e64
7 changed files with 34 additions and 11 deletions

View File

@ -45,8 +45,8 @@
<strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>
@await FlushAsync()
@{
await FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
}
<div class="row">

View File

@ -24,7 +24,7 @@
</div>
</div>
<div class="container body-content">
@{ await FlushAsync(); }
@await FlushAsync()
@RenderBody()
@RenderSection("content")
<hr />

View File

@ -544,8 +544,11 @@ namespace Microsoft.AspNet.Mvc.Razor
/// Invokes <see cref="TextWriter.FlushAsync"/> on <see cref="Output"/> writing out any buffered
/// content to the <see cref="HttpResponse.Body"/>.
/// </summary>
/// <returns>A <see cref="Task"/> that represents the asynchronous flush operation.</returns>
public Task FlushAsync()
/// <returns>A<see cref="Task{HtmlString}"/> that represents the asynchronous flush operation and on
/// completion returns a <see cref="HtmlString.Empty"/>.</returns>
/// <remarks>The value returned is a token value that allows FlushAsync to succeed. However the
/// value does not represent the rendered content.</remarks>
public async Task<HtmlString> FlushAsync()
{
// If there are active writing scopes then we should throw. Cannot flush content that has the potential to
// change.
@ -562,7 +565,8 @@ namespace Microsoft.AspNet.Mvc.Razor
throw new InvalidOperationException(message);
}
return Output.FlushAsync();
await Output.FlushAsync();
return HtmlString.Empty;
}
/// <inheritdoc />

View File

@ -98,10 +98,10 @@ namespace Microsoft.AspNet.Mvc.Razor
{
// Arrange
var viewContext = CreateViewContext();
var page = CreatePage(v =>
var page = CreatePage(async v =>
{
v.StartWritingScope();
v.FlushAsync();
await v.FlushAsync();
});
// Act
@ -527,6 +527,25 @@ namespace Microsoft.AspNet.Mvc.Razor
await Assert.DoesNotThrowAsync(() => renderAsyncDelegate(TextWriter.Null));
}
[Fact]
public async Task FlushAsync_ReturnsEmptyHtmlString()
{
// Arrange
HtmlString actual = null;
var writer = new Mock<TextWriter>();
var context = CreateViewContext(writer.Object);
var page = CreatePage(async p =>
{
actual = await p.FlushAsync();
}, context);
// Act
await page.ExecuteAsync();
// Assert
Assert.Same(HtmlString.Empty, actual);
}
[Fact]
public async Task WriteAttribute_CallsBeginAndEndContext_OnPageExecutionListenerContext()
{

View File

@ -1,12 +1,12 @@
@inject WaitService WaitService
Initial content
@await FlushAsync()
@{
await FlushAsync();
WaitService.WaitForClient();
}
Secondary content
@await FlushAsync()
@{
await FlushAsync();
WaitService.WaitForClient();
}
Final content

View File

@ -1,7 +1,7 @@
@inject WaitService WaitService
<title>@ViewBag.Title</title>
@await FlushAsync()
@{
await FlushAsync();
WaitService.WaitForClient();
}
@RenderBody()

View File

@ -1,8 +1,8 @@
@inject WaitService WaitService
<title>@ViewBag.Title</title>
@RenderBody()
@await FlushAsync()
@{
await FlushAsync();
WaitService.WaitForClient();
}
@await Html.PartialAsync("_PartialThatSetsTitle")