This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
HelperResult.cs
61 lines (53 loc) · 2.17 KB
/
HelperResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
namespace Microsoft.AspNetCore.Mvc.Razor
{
/// <summary>
/// Represents a deferred write operation in a <see cref="RazorPage"/>.
/// </summary>
public class HelperResult : IHtmlContent
{
private readonly Func<TextWriter, Task> _asyncAction;
/// <summary>
/// Creates a new instance of <see cref="HelperResult"/>.
/// </summary>
/// <param name="asyncAction">The asynchronous delegate to invoke when
/// <see cref="WriteTo(TextWriter, HtmlEncoder)"/> is called.</param>
/// <remarks>Calls to <see cref="WriteTo(TextWriter, HtmlEncoder)"/> result in a blocking invocation of
/// <paramref name="asyncAction"/>.</remarks>
public HelperResult(Func<TextWriter, Task> asyncAction)
{
if (asyncAction == null)
{
throw new ArgumentNullException(nameof(asyncAction));
}
_asyncAction = asyncAction;
}
/// <summary>
/// Gets the asynchronous delegate to invoke when <see cref="WriteTo(TextWriter, HtmlEncoder)"/> is called.
/// </summary>
public Func<TextWriter, Task> WriteAction => _asyncAction;
/// <summary>
/// Method invoked to produce content from the <see cref="HelperResult"/>.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param>
/// <param name="encoder">The <see cref="HtmlEncoder"/> to encode the content.</param>
public virtual void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}
_asyncAction(writer).GetAwaiter().GetResult();
}
}
}