|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Reflection; |
| 5 | +using Microsoft.AspNetCore.Components; |
| 6 | +using Microsoft.AspNetCore.Components.Rendering; |
| 7 | +using Microsoft.Extensions.Internal; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Mvc.ViewFeatures; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Internal component type that acts as a root component when executing a RazorComponentResult. It takes |
| 13 | +/// care of rendering the component inside its hierarchy of layouts (via LayoutView) as well as converting |
| 14 | +/// any information we want from RazorComponentResult into component parameters. We could also use this to |
| 15 | +/// supply other data from the original HttpContext as component parameters, e.g., for model binding. |
| 16 | +/// |
| 17 | +/// It happens to be almost the same as RouteView except it doesn't supply any query parameters. We can |
| 18 | +/// resolve that at the same time we implement support for form posts. |
| 19 | +/// </summary> |
| 20 | +internal class RazorComponentResultHost : IComponent |
| 21 | +{ |
| 22 | + private RenderHandle _renderHandle; |
| 23 | + |
| 24 | + public RazorComponentResult RazorComponentResult { get; private set; } |
| 25 | + |
| 26 | + public void Attach(RenderHandle renderHandle) |
| 27 | + => _renderHandle = renderHandle; |
| 28 | + |
| 29 | + public Task SetParametersAsync(ParameterView parameters) |
| 30 | + { |
| 31 | + foreach (var kvp in parameters) |
| 32 | + { |
| 33 | + switch (kvp.Name) |
| 34 | + { |
| 35 | + case nameof(RazorComponentResult): |
| 36 | + RazorComponentResult = (RazorComponentResult)kvp.Value; |
| 37 | + break; |
| 38 | + default: |
| 39 | + throw new ArgumentException($"Unknown parameter '{kvp.Name}'"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (RazorComponentResult is null) |
| 44 | + { |
| 45 | + throw new InvalidOperationException($"Failed to supply nonnull value for required parameter '{nameof(RazorComponentResult)}'"); |
| 46 | + } |
| 47 | + |
| 48 | + _renderHandle.Render(BuildRenderTree); |
| 49 | + return Task.CompletedTask; |
| 50 | + } |
| 51 | + |
| 52 | + private void BuildRenderTree(RenderTreeBuilder builder) |
| 53 | + { |
| 54 | + var pageLayoutType = RazorComponentResult.ComponentType.GetCustomAttribute<LayoutAttribute>()?.LayoutType; |
| 55 | + |
| 56 | + builder.OpenComponent<LayoutView>(0); |
| 57 | + builder.AddComponentParameter(1, nameof(LayoutView.Layout), pageLayoutType); |
| 58 | + builder.AddComponentParameter(2, nameof(LayoutView.ChildContent), (RenderFragment)RenderPageWithParameters); |
| 59 | + builder.CloseComponent(); |
| 60 | + } |
| 61 | + |
| 62 | + private void RenderPageWithParameters(RenderTreeBuilder builder) |
| 63 | + { |
| 64 | + builder.OpenComponent(0, RazorComponentResult.ComponentType); |
| 65 | + |
| 66 | + if (RazorComponentResult.Parameters is not null) |
| 67 | + { |
| 68 | + var dict = PropertyHelper.ObjectToDictionary(RazorComponentResult.Parameters); |
| 69 | + foreach (var kvp in dict) |
| 70 | + { |
| 71 | + builder.AddComponentParameter(1, kvp.Key, kvp.Value); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + builder.CloseComponent(); |
| 76 | + } |
| 77 | +} |
0 commit comments