-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
FormValueProvider.cs
105 lines (88 loc) · 3.26 KB
/
FormValueProvider.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
// 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.Globalization;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Mvc.ModelBinding;
/// <summary>
/// An <see cref="IValueProvider"/> adapter for data stored in an <see cref="IFormCollection"/>.
/// </summary>
public class FormValueProvider : BindingSourceValueProvider, IEnumerableValueProvider
{
private readonly IFormCollection _values;
private readonly HashSet<string?>? _invariantValueKeys;
private PrefixContainer? _prefixContainer;
/// <summary>
/// Creates a value provider for <see cref="IFormCollection"/>.
/// </summary>
/// <param name="bindingSource">The <see cref="BindingSource"/> for the data.</param>
/// <param name="values">The key value pairs to wrap.</param>
/// <param name="culture">The culture to return with ValueProviderResult instances.</param>
public FormValueProvider(
BindingSource bindingSource,
IFormCollection values,
CultureInfo? culture)
: base(bindingSource)
{
ArgumentNullException.ThrowIfNull(bindingSource);
ArgumentNullException.ThrowIfNull(values);
_values = values;
if (_values.TryGetValue(FormValueHelper.CultureInvariantFieldName, out var invariantKeys) && invariantKeys.Count > 0)
{
_invariantValueKeys = new(invariantKeys, StringComparer.OrdinalIgnoreCase);
}
Culture = culture;
}
/// <summary>
/// The culture to use.
/// </summary>
public CultureInfo? Culture { get; }
/// <summary>
/// The prefix container.
/// </summary>
protected PrefixContainer PrefixContainer
{
get
{
if (_prefixContainer == null)
{
_prefixContainer = new PrefixContainer(_values.Keys);
}
return _prefixContainer;
}
}
/// <inheritdoc />
public override bool ContainsPrefix(string prefix)
{
return PrefixContainer.ContainsPrefix(prefix);
}
/// <inheritdoc />
public virtual IDictionary<string, string> GetKeysFromPrefix(string prefix)
{
ArgumentNullException.ThrowIfNull(prefix);
return PrefixContainer.GetKeysFromPrefix(prefix);
}
/// <inheritdoc />
public override ValueProviderResult GetValue(string key)
{
ArgumentNullException.ThrowIfNull(key);
if (key.Length == 0)
{
// Top level parameters will fall back to an empty prefix when the parameter name does not
// appear in any value provider. This would result in the parameter binding to a form parameter
// with a empty key (e.g. Request body looks like "=test") which isn't a scenario we want to support.
// Return a "None" result in this event.
return ValueProviderResult.None;
}
var values = _values[key];
if (values.Count == 0)
{
return ValueProviderResult.None;
}
else
{
var culture = _invariantValueKeys?.Contains(key) == true ? CultureInfo.InvariantCulture : Culture;
return new ValueProviderResult(values, culture);
}
}
}