-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
ViewComponentResultExecutor.cs
201 lines (174 loc) · 7.72 KB
/
ViewComponentResultExecutor.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// 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;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Filters;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures;
/// <summary>
/// A <see cref="IActionResultExecutor{ViewComponentResult}"/> for <see cref="ViewComponentResult"/>.
/// </summary>
public partial class ViewComponentResultExecutor : IActionResultExecutor<ViewComponentResult>
{
private readonly HtmlEncoder _htmlEncoder;
private readonly HtmlHelperOptions _htmlHelperOptions;
private readonly ILogger<ViewComponentResult> _logger;
private readonly IModelMetadataProvider _modelMetadataProvider;
private readonly ITempDataDictionaryFactory _tempDataDictionaryFactory;
private readonly IHttpResponseStreamWriterFactory _writerFactory;
/// <summary>
/// Initialize a new instance of <see cref="ViewComponentResultExecutor"/>
/// </summary>
/// <param name="mvcHelperOptions">The <see cref="IOptions{MvcViewOptions}"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="htmlEncoder">The <see cref="HtmlEncoder"/>.</param>
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param>
/// <param name="tempDataDictionaryFactory">The <see cref="ITempDataDictionaryFactory"/>.</param>
/// <param name="writerFactory">The <see cref=" IHttpResponseStreamWriterFactory"/>.</param>
public ViewComponentResultExecutor(
IOptions<MvcViewOptions> mvcHelperOptions,
ILoggerFactory loggerFactory,
HtmlEncoder htmlEncoder,
IModelMetadataProvider modelMetadataProvider,
ITempDataDictionaryFactory tempDataDictionaryFactory,
IHttpResponseStreamWriterFactory writerFactory)
{
if (mvcHelperOptions == null)
{
throw new ArgumentNullException(nameof(mvcHelperOptions));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
if (htmlEncoder == null)
{
throw new ArgumentNullException(nameof(htmlEncoder));
}
if (modelMetadataProvider == null)
{
throw new ArgumentNullException(nameof(modelMetadataProvider));
}
if (tempDataDictionaryFactory == null)
{
throw new ArgumentNullException(nameof(tempDataDictionaryFactory));
}
_htmlHelperOptions = mvcHelperOptions.Value.HtmlHelperOptions;
_logger = loggerFactory.CreateLogger<ViewComponentResult>();
_htmlEncoder = htmlEncoder;
_modelMetadataProvider = modelMetadataProvider;
_tempDataDictionaryFactory = tempDataDictionaryFactory;
_writerFactory = writerFactory;
}
/// <inheritdoc />
public virtual async Task ExecuteAsync(ActionContext context, ViewComponentResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
var response = context.HttpContext.Response;
var viewData = result.ViewData;
if (viewData == null)
{
viewData = new ViewDataDictionary(_modelMetadataProvider, context.ModelState);
}
var tempData = result.TempData;
if (tempData == null)
{
tempData = _tempDataDictionaryFactory.GetTempData(context.HttpContext);
}
ResponseContentTypeHelper.ResolveContentTypeAndEncoding(
result.ContentType,
response.ContentType,
(ViewExecutor.DefaultContentType, Encoding.UTF8),
MediaType.GetEncoding,
out var resolvedContentType,
out var resolvedContentTypeEncoding);
response.ContentType = resolvedContentType;
if (result.StatusCode != null)
{
response.StatusCode = result.StatusCode.Value;
}
await using var writer = _writerFactory.CreateWriter(response.Body, resolvedContentTypeEncoding);
var viewContext = new ViewContext(
context,
NullView.Instance,
viewData,
tempData,
writer,
_htmlHelperOptions);
OnExecuting(viewContext);
// IViewComponentHelper is stateful, we want to make sure to retrieve it every time we need it.
var viewComponentHelper = context.HttpContext.RequestServices.GetRequiredService<IViewComponentHelper>();
(viewComponentHelper as IViewContextAware)?.Contextualize(viewContext);
var viewComponentResult = await GetViewComponentResult(viewComponentHelper, _logger, result);
if (viewComponentResult is ViewBuffer viewBuffer)
{
// In the ordinary case, DefaultViewComponentHelper will return an instance of ViewBuffer. We can simply
// invoke WriteToAsync on it.
await viewBuffer.WriteToAsync(writer, _htmlEncoder);
await writer.FlushAsync();
}
else
{
await using var bufferingStream = new FileBufferingWriteStream();
await using (var intermediateWriter = _writerFactory.CreateWriter(bufferingStream, resolvedContentTypeEncoding))
{
viewComponentResult.WriteTo(intermediateWriter, _htmlEncoder);
}
await bufferingStream.DrainBufferAsync(response.Body);
}
}
private static void OnExecuting(ViewContext viewContext)
{
var viewDataValuesProvider = viewContext.HttpContext.Features.Get<IViewDataValuesProviderFeature>();
if (viewDataValuesProvider != null)
{
viewDataValuesProvider.ProvideViewDataValues(viewContext.ViewData);
}
}
private static Task<IHtmlContent> GetViewComponentResult(IViewComponentHelper viewComponentHelper, ILogger logger, ViewComponentResult result)
{
if (result.ViewComponentType == null && result.ViewComponentName == null)
{
throw new InvalidOperationException(Resources.FormatViewComponentResult_NameOrTypeMustBeSet(
nameof(ViewComponentResult.ViewComponentName),
nameof(ViewComponentResult.ViewComponentType)));
}
else if (result.ViewComponentType == null)
{
Log.ViewComponentResultExecuting(logger, result.ViewComponentName);
return viewComponentHelper.InvokeAsync(result.ViewComponentName!, result.Arguments);
}
else
{
Log.ViewComponentResultExecuting(logger, result.ViewComponentType);
return viewComponentHelper.InvokeAsync(result.ViewComponentType, result.Arguments);
}
}
private static partial class Log
{
[LoggerMessage(1, LogLevel.Information, "Executing ViewComponentResult, running {ViewComponentName}.", EventName = "ViewComponentResultExecuting")]
public static partial void ViewComponentResultExecuting(ILogger logger, string? viewComponentName);
public static void ViewComponentResultExecuting(ILogger logger, Type viewComponentType)
{
ViewComponentResultExecuting(logger, viewComponentType.Name);
}
}
}