-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
ViewComponentContext.cs
120 lines (106 loc) · 4.1 KB
/
ViewComponentContext.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace Microsoft.AspNetCore.Mvc.ViewComponents;
/// <summary>
/// A context for view components.
/// </summary>
public class ViewComponentContext
{
/// <summary>
/// Creates a new <see cref="ViewComponentContext"/>.
/// </summary>
/// <remarks>
/// The default constructor is provided for unit test purposes only.
/// </remarks>
public ViewComponentContext()
{
ViewComponentDescriptor = new ViewComponentDescriptor();
ViewContext = new ViewContext();
}
/// <summary>
/// Creates a new <see cref="ViewComponentContext"/>.
/// </summary>
/// <param name="viewComponentDescriptor">
/// The <see cref="ViewComponentContext"/> for the view component being invoked.
/// </param>
/// <param name="arguments">The view component arguments.</param>
/// <param name="htmlEncoder">The <see cref="HtmlEncoder"/> to use.</param>
/// <param name="viewContext">The <see cref="ViewContext"/>.</param>
/// <param name="writer">The <see cref="TextWriter"/> for writing output.</param>
public ViewComponentContext(
ViewComponentDescriptor viewComponentDescriptor,
IDictionary<string, object?> arguments,
HtmlEncoder htmlEncoder,
ViewContext viewContext,
TextWriter writer)
{
ArgumentNullException.ThrowIfNull(viewComponentDescriptor);
ArgumentNullException.ThrowIfNull(arguments);
ArgumentNullException.ThrowIfNull(htmlEncoder);
ArgumentNullException.ThrowIfNull(viewContext);
ArgumentNullException.ThrowIfNull(writer);
ViewComponentDescriptor = viewComponentDescriptor;
Arguments = arguments;
HtmlEncoder = htmlEncoder;
// We want to create a defensive copy of the VDD here so that changes done in the VC
// aren't visible in the calling view.
ViewContext = new ViewContext(
viewContext,
viewContext.View,
new ViewDataDictionary<object>(viewContext.ViewData),
writer);
}
/// <summary>
/// Gets or sets the view component arguments.
/// </summary>
/// <remarks>
/// The property setter is provided for unit test purposes only.
/// </remarks>
public IDictionary<string, object?> Arguments { get; set; } = default!;
/// <summary>
/// Gets or sets the <see cref="System.Text.Encodings.Web.HtmlEncoder"/>.
/// </summary>
/// <remarks>
/// The property setter is provided for unit test purposes only.
/// </remarks>
public HtmlEncoder HtmlEncoder { get; set; } = default!;
/// <summary>
/// Gets or sets the <see cref="ViewComponents.ViewComponentDescriptor"/> for the view component being invoked.
/// </summary>
/// <remarks>
/// The property setter is provided for unit test purposes only.
/// </remarks>
public ViewComponentDescriptor ViewComponentDescriptor { get; set; }
/// <summary>
/// Gets or sets the <see cref="Rendering.ViewContext"/>.
/// </summary>
/// <remarks>
/// The property setter is provided for unit test purposes only.
/// </remarks>
public ViewContext ViewContext { get; set; }
/// <summary>
/// Gets the <see cref="ViewDataDictionary"/>.
/// </summary>
/// <remarks>
/// This is an alias for <c>ViewContext.ViewData</c>.
/// </remarks>
public ViewDataDictionary ViewData => ViewContext.ViewData;
/// <summary>
/// Gets the <see cref="ITempDataDictionary"/>.
/// </summary>
/// <remarks>
/// This is an alias for <c>ViewContext.TempData</c>.
/// </remarks>
public ITempDataDictionary TempData => ViewContext.TempData;
/// <summary>
/// Gets the <see cref="TextWriter"/> for output.
/// </summary>
/// <remarks>
/// This is an alias for <c>ViewContext.Writer</c>.
/// </remarks>
public TextWriter Writer => ViewContext.Writer;
}