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
/
ContentViewComponentResult.cs
63 lines (55 loc) · 2.04 KB
/
ContentViewComponentResult.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
62
63
// 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.Threading.Tasks;
namespace Microsoft.AspNetCore.Mvc.ViewComponents
{
/// <summary>
/// An <see cref="IViewComponentResult"/> which writes text when executed.
/// </summary>
/// <remarks>
/// The provided content will be HTML-encoded when written. To write pre-encoded content, use an
/// <see cref="HtmlContentViewComponentResult"/>.
/// </remarks>
public class ContentViewComponentResult : IViewComponentResult
{
/// <summary>
/// Initializes a new <see cref="ContentViewComponentResult"/>.
/// </summary>
/// <param name="content">Content to write. The content will be HTML encoded when written.</param>
public ContentViewComponentResult(string content)
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
Content = content;
}
/// <summary>
/// Gets the content.
/// </summary>
public string Content { get; }
/// <summary>
/// Encodes and writes the <see cref="Content"/>.
/// </summary>
/// <param name="context">The <see cref="ViewComponentContext"/>.</param>
public void Execute(ViewComponentContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
context.HtmlEncoder.Encode(context.Writer, Content);
}
/// <summary>
/// Encodes and writes the <see cref="Content"/>.
/// </summary>
/// <param name="context">The <see cref="ViewComponentContext"/>.</param>
/// <returns>A completed <see cref="Task"/>.</returns>
public Task ExecuteAsync(ViewComponentContext context)
{
Execute(context);
return Task.CompletedTask;
}
}
}