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> <strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address> </address>
@await FlushAsync()
@{ @{
await FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(1)); await Task.Delay(TimeSpan.FromSeconds(1));
} }
<div class="row"> <div class="row">

View File

@ -24,7 +24,7 @@
</div> </div>
</div> </div>
<div class="container body-content"> <div class="container body-content">
@{ await FlushAsync(); } @await FlushAsync()
@RenderBody() @RenderBody()
@RenderSection("content") @RenderSection("content")
<hr /> <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 /// Invokes <see cref="TextWriter.FlushAsync"/> on <see cref="Output"/> writing out any buffered
/// content to the <see cref="HttpResponse.Body"/>. /// content to the <see cref="HttpResponse.Body"/>.
/// </summary> /// </summary>
/// <returns>A <see cref="Task"/> that represents the asynchronous flush operation.</returns> /// <returns>A<see cref="Task{HtmlString}"/> that represents the asynchronous flush operation and on
public Task FlushAsync() /// 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 // If there are active writing scopes then we should throw. Cannot flush content that has the potential to
// change. // change.
@ -562,7 +565,8 @@ namespace Microsoft.AspNet.Mvc.Razor
throw new InvalidOperationException(message); throw new InvalidOperationException(message);
} }
return Output.FlushAsync(); await Output.FlushAsync();
return HtmlString.Empty;
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -98,10 +98,10 @@ namespace Microsoft.AspNet.Mvc.Razor
{ {
// Arrange // Arrange
var viewContext = CreateViewContext(); var viewContext = CreateViewContext();
var page = CreatePage(v => var page = CreatePage(async v =>
{ {
v.StartWritingScope(); v.StartWritingScope();
v.FlushAsync(); await v.FlushAsync();
}); });
// Act // Act
@ -527,6 +527,25 @@ namespace Microsoft.AspNet.Mvc.Razor
await Assert.DoesNotThrowAsync(() => renderAsyncDelegate(TextWriter.Null)); 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] [Fact]
public async Task WriteAttribute_CallsBeginAndEndContext_OnPageExecutionListenerContext() public async Task WriteAttribute_CallsBeginAndEndContext_OnPageExecutionListenerContext()
{ {

View File

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

View File

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

View File

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