This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
RouteValueProvider.cs
103 lines (90 loc) · 3.31 KB
/
RouteValueProvider.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
/// <summary>
/// An <see cref="IValueProvider"/> adapter for data stored in an <see cref="RouteValueDictionary"/>.
/// </summary>
public class RouteValueProvider : BindingSourceValueProvider
{
private readonly RouteValueDictionary _values;
private PrefixContainer _prefixContainer;
/// <summary>
/// Creates a new <see cref="RouteValueProvider"/>.
/// </summary>
/// <param name="bindingSource">The <see cref="BindingSource"/> of the data.</param>
/// <param name="values">The values.</param>
/// <remarks>Sets <see cref="Culture"/> to <see cref="CultureInfo.InvariantCulture" />.</remarks>
public RouteValueProvider(
BindingSource bindingSource,
RouteValueDictionary values)
: this(bindingSource, values, CultureInfo.InvariantCulture)
{
}
/// <summary>
/// Creates a new <see cref="RouteValueProvider"/>.
/// </summary>
/// <param name="bindingSource">The <see cref="BindingSource"/> of the data.</param>
/// <param name="values">The values.</param>
/// <param name="culture">The culture for route value.</param>
public RouteValueProvider(BindingSource bindingSource, RouteValueDictionary values, CultureInfo culture)
: base(bindingSource)
{
if (bindingSource == null)
{
throw new ArgumentNullException(nameof(bindingSource));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
_values = values;
Culture = culture;
}
#pragma warning disable PUB0001 // Pubternal type in public API
protected PrefixContainer PrefixContainer
#pragma warning restore PUB0001
{
get
{
if (_prefixContainer == null)
{
_prefixContainer = new PrefixContainer(_values.Keys);
}
return _prefixContainer;
}
}
protected CultureInfo Culture { get; }
/// <inheritdoc />
public override bool ContainsPrefix(string key)
{
return PrefixContainer.ContainsPrefix(key);
}
/// <inheritdoc />
public override ValueProviderResult GetValue(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
object value;
if (_values.TryGetValue(key, out value))
{
var stringValue = value as string ?? value?.ToString() ?? string.Empty;
return new ValueProviderResult(stringValue, Culture);
}
else
{
return ValueProviderResult.None;
}
}
}
}