1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Collections.Specialized;
\r
17 using System.Globalization;
\r
20 public class NameValueCollectionValueProvider : IValueProvider {
\r
22 private readonly HashSet<string> _prefixes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
\r
23 private readonly Dictionary<string, ValueProviderResult> _values = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);
\r
25 public NameValueCollectionValueProvider(NameValueCollection collection, CultureInfo culture) {
\r
26 if (collection == null) {
\r
27 throw new ArgumentNullException("collection");
\r
30 AddValues(collection, culture);
\r
33 private void AddValues(NameValueCollection collection, CultureInfo culture) {
\r
34 if (collection.Count > 0) {
\r
38 foreach (string key in collection) {
\r
40 _prefixes.UnionWith(ValueProviderUtil.GetPrefixes(key));
\r
42 string[] rawValue = collection.GetValues(key);
\r
43 string attemptedValue = collection[key];
\r
44 _values[key] = new ValueProviderResult(rawValue, attemptedValue, culture);
\r
49 public virtual bool ContainsPrefix(string prefix) {
\r
50 if (prefix == null) {
\r
51 throw new ArgumentNullException("prefix");
\r
54 return _prefixes.Contains(prefix);
\r
57 public virtual ValueProviderResult GetValue(string key) {
\r
59 throw new ArgumentNullException("key");
\r
62 ValueProviderResult vpResult;
\r
63 _values.TryGetValue(key, out vpResult);
\r