Skip to content

Commit 3b091f7

Browse files
authored
[Blazor] Add support for accessing the request body form parameters (#47814)
Adds support for accessing the request body form parameters via a new `FormDataProvider` service.
1 parent 08f6eea commit 3b091f7

32 files changed

+304
-27
lines changed

src/Components/Authorization/test/AuthorizeRouteViewTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Security.Claims;
55
using Microsoft.AspNetCore.Authorization;
6-
using Microsoft.AspNetCore.Components.Binding;
76
using Microsoft.AspNetCore.Components.Rendering;
87
using Microsoft.AspNetCore.Components.RenderTree;
98
using Microsoft.AspNetCore.Components.Test.Helpers;

src/Components/Components/src/Binding/CascadingModelBinder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Reflection.Metadata;
5-
using Microsoft.AspNetCore.Components.Binding;
65
using Microsoft.AspNetCore.Components.Routing;
76

87
namespace Microsoft.AspNetCore.Components;

src/Components/Components/src/Binding/ModelBindingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.AspNetCore.Components.Binding;
4+
namespace Microsoft.AspNetCore.Components;
55

66
/// <summary>
77
/// The binding context associated with a given model binding operation.

src/Components/Components/src/PublicAPI.Unshipped.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#nullable enable
2-
Microsoft.AspNetCore.Components.Binding.ModelBindingContext
3-
Microsoft.AspNetCore.Components.Binding.ModelBindingContext.BindingContextId.get -> string!
4-
Microsoft.AspNetCore.Components.Binding.ModelBindingContext.Name.get -> string!
52
Microsoft.AspNetCore.Components.CascadingModelBinder
63
Microsoft.AspNetCore.Components.CascadingModelBinder.CascadingModelBinder() -> void
7-
Microsoft.AspNetCore.Components.CascadingModelBinder.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Binding.ModelBindingContext!>!
4+
Microsoft.AspNetCore.Components.CascadingModelBinder.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.ModelBindingContext!>!
85
Microsoft.AspNetCore.Components.CascadingModelBinder.ChildContent.set -> void
96
Microsoft.AspNetCore.Components.CascadingModelBinder.IsFixed.get -> bool
107
Microsoft.AspNetCore.Components.CascadingModelBinder.IsFixed.set -> void
118
Microsoft.AspNetCore.Components.CascadingModelBinder.Name.get -> string!
129
Microsoft.AspNetCore.Components.CascadingModelBinder.Name.set -> void
1310
Microsoft.AspNetCore.Components.ComponentBase.DispatchExceptionAsync(System.Exception! exception) -> System.Threading.Tasks.Task!
1411
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.PersistStateAsync(Microsoft.AspNetCore.Components.IPersistentComponentStateStore! store, Microsoft.AspNetCore.Components.Dispatcher! dispatcher) -> System.Threading.Tasks.Task!
12+
Microsoft.AspNetCore.Components.ModelBindingContext
13+
Microsoft.AspNetCore.Components.ModelBindingContext.BindingContextId.get -> string!
14+
Microsoft.AspNetCore.Components.ModelBindingContext.Name.get -> string!
1515
Microsoft.AspNetCore.Components.RenderHandle.DispatchExceptionAsync(System.Exception! exception) -> System.Threading.Tasks.Task!
1616
*REMOVED*Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string! relativeUri) -> System.Uri!
1717
Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string? relativeUri) -> System.Uri!

src/Components/Components/src/RouteView.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using System.Diagnostics.CodeAnalysis;
77
using System.Reflection;
8-
using Microsoft.AspNetCore.Components.Binding;
98
using Microsoft.AspNetCore.Components.Rendering;
109
using Microsoft.AspNetCore.Components.Routing;
1110

src/Components/Components/test/CascadingModelBinderTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics.CodeAnalysis;
5-
using Microsoft.AspNetCore.Components.Binding;
65
using Microsoft.AspNetCore.Components.Rendering;
76
using Microsoft.AspNetCore.Components.Test.Helpers;
87
using Microsoft.Extensions.DependencyInjection;

src/Components/Components/test/ModelBindingContextTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Microsoft.AspNetCore.Components.Binding;
5-
64
namespace Microsoft.AspNetCore.Components;
75

86
public class ModelBindingContextTest

src/Components/Components/test/RouteViewTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Microsoft.AspNetCore.Components.Binding;
54
using Microsoft.AspNetCore.Components.Rendering;
65
using Microsoft.AspNetCore.Components.Test.Helpers;
76
using Microsoft.Extensions.DependencyInjection;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.Collections.ObjectModel;
5+
using Microsoft.AspNetCore.Components.Forms;
6+
using Microsoft.Extensions.Primitives;
7+
8+
namespace Microsoft.AspNetCore.Components.Endpoints;
9+
10+
internal sealed class HttpContextFormDataProvider : FormDataProvider, IHostEnvironmentFormDataProvider
11+
{
12+
private string? _name;
13+
private IReadOnlyDictionary<string, StringValues>? _entries;
14+
15+
public override string? Name => _name;
16+
17+
public override IReadOnlyDictionary<string, StringValues> Entries => _entries ?? ReadOnlyDictionary<string, StringValues>.Empty;
18+
19+
void IHostEnvironmentFormDataProvider.SetFormData(string name, IReadOnlyDictionary<string, StringValues> form)
20+
{
21+
_name = name;
22+
_entries = form;
23+
}
24+
}

src/Components/Endpoints/src/DependencyInjection/RazorComponentsServiceCollectionExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.CodeAnalysis;
55
using Microsoft.AspNetCore.Components;
66
using Microsoft.AspNetCore.Components.Endpoints;
7+
using Microsoft.AspNetCore.Components.Forms;
78
using Microsoft.AspNetCore.Components.Infrastructure;
89
using Microsoft.AspNetCore.Components.Routing;
910
using Microsoft.AspNetCore.Components.Web;
@@ -50,6 +51,9 @@ public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection
5051
services.TryAddScoped<IErrorBoundaryLogger, PrerenderingErrorBoundaryLogger>();
5152
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<RazorComponentsEndpointsOptions>, RazorComponentsEndpointsDetailedErrorsConfiguration>());
5253

54+
// Form handling
55+
services.TryAddScoped<FormDataProvider, HttpContextFormDataProvider>();
56+
5357
return new DefaultRazorComponentsBuilder(services);
5458
}
5559

0 commit comments

Comments
 (0)