/mcs/class/System.Web.Mvc2/System.Web.Mvc/ValueProviderDictionary.cs
C# | 208 lines | 159 code | 34 blank | 15 comment | 14 complexity | 6788e77fbd989665f0216c43b0a6703a MD5 | raw file
1/* ****************************************************************************
2 *
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 *
5 * This software is subject to the Microsoft Public License (Ms-PL).
6 * A copy of the license can be found in the license.htm file included
7 * in this distribution.
8 *
9 * You must not remove this notice, or any other, from this software.
10 *
11 * ***************************************************************************/
12
13namespace System.Web.Mvc {
14 using System;
15 using System.Collections;
16 using System.Collections.Generic;
17 using System.Collections.Specialized;
18 using System.Diagnostics.CodeAnalysis;
19 using System.Globalization;
20 using System.Web.Routing;
21
22 [Obsolete("The recommended alternative is to use one of the specific ValueProvider types, such as FormValueProvider.")]
23 public class ValueProviderDictionary : IDictionary<string, ValueProviderResult>, IValueProvider {
24
25 private readonly Dictionary<string, ValueProviderResult> _dictionary = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);
26
27 public ValueProviderDictionary(ControllerContext controllerContext) {
28 ControllerContext = controllerContext;
29 if (controllerContext != null) {
30 PopulateDictionary();
31 }
32 }
33
34 public ControllerContext ControllerContext {
35 get;
36 private set;
37 }
38
39 public int Count {
40 get {
41 return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Count;
42 }
43 }
44
45 internal Dictionary<string, ValueProviderResult> Dictionary {
46 get {
47 return _dictionary;
48 }
49 }
50
51 public bool IsReadOnly {
52 get {
53 return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).IsReadOnly;
54 }
55 }
56
57 public ICollection<string> Keys {
58 get {
59 return Dictionary.Keys;
60 }
61 }
62
63 public ValueProviderResult this[string key] {
64 get {
65 ValueProviderResult result;
66 Dictionary.TryGetValue(key, out result);
67 return result;
68 }
69 set {
70 Dictionary[key] = value;
71 }
72 }
73
74 public ICollection<ValueProviderResult> Values {
75 get {
76 return Dictionary.Values;
77 }
78 }
79
80 public void Add(KeyValuePair<string, ValueProviderResult> item) {
81 ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Add(item);
82 }
83
84 public void Add(string key, object value) {
85 string attemptedValue = Convert.ToString(value, CultureInfo.InvariantCulture);
86 ValueProviderResult vpResult = new ValueProviderResult(value, attemptedValue, CultureInfo.InvariantCulture);
87 Add(key, vpResult);
88 }
89
90 public void Add(string key, ValueProviderResult value) {
91 Dictionary.Add(key, value);
92 }
93
94 private void AddToDictionaryIfNotPresent(string key, ValueProviderResult result) {
95 if (!String.IsNullOrEmpty(key)) {
96 if (!Dictionary.ContainsKey(key)) {
97 Dictionary.Add(key, result);
98 }
99 }
100 }
101
102 public void Clear() {
103 ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Clear();
104 }
105
106 public bool Contains(KeyValuePair<string, ValueProviderResult> item) {
107 return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Contains(item);
108 }
109
110 public bool ContainsKey(string key) {
111 return Dictionary.ContainsKey(key);
112 }
113
114 public void CopyTo(KeyValuePair<string, ValueProviderResult>[] array, int arrayIndex) {
115 ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).CopyTo(array, arrayIndex);
116 }
117
118 public IEnumerator<KeyValuePair<string, ValueProviderResult>> GetEnumerator() {
119 return ((IEnumerable<KeyValuePair<string, ValueProviderResult>>)Dictionary).GetEnumerator();
120 }
121
122 private void PopulateDictionary() {
123 CultureInfo currentCulture = CultureInfo.CurrentCulture;
124 CultureInfo invariantCulture = CultureInfo.InvariantCulture;
125
126 // We use this order of precedence to populate the dictionary:
127 // 1. Request form submission (should be culture-aware)
128 // 2. Values from the RouteData (could be from the typed-in URL or from the route's default values)
129 // 3. URI query string
130
131 NameValueCollection form = ControllerContext.HttpContext.Request.Form;
132 if (form != null) {
133 string[] keys = form.AllKeys;
134 foreach (string key in keys) {
135 string[] rawValue = form.GetValues(key);
136 string attemptedValue = form[key];
137 ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, currentCulture);
138 AddToDictionaryIfNotPresent(key, result);
139 }
140 }
141
142 RouteValueDictionary routeValues = ControllerContext.RouteData.Values;
143 if (routeValues != null) {
144 foreach (var kvp in routeValues) {
145 string key = kvp.Key;
146 object rawValue = kvp.Value;
147 string attemptedValue = Convert.ToString(rawValue, invariantCulture);
148 ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
149 AddToDictionaryIfNotPresent(key, result);
150 }
151 }
152
153 NameValueCollection queryString = ControllerContext.HttpContext.Request.QueryString;
154 if (queryString != null) {
155 string[] keys = queryString.AllKeys;
156 foreach (string key in keys) {
157 string[] rawValue = queryString.GetValues(key);
158 string attemptedValue = queryString[key];
159 ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
160 AddToDictionaryIfNotPresent(key, result);
161 }
162 }
163 }
164
165 public bool Remove(KeyValuePair<string, ValueProviderResult> item) {
166 return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Remove(item);
167 }
168
169 public bool Remove(string key) {
170 return Dictionary.Remove(key);
171 }
172
173 public bool TryGetValue(string key, out ValueProviderResult value) {
174 return Dictionary.TryGetValue(key, out value);
175 }
176
177 #region IEnumerable Members
178 IEnumerator IEnumerable.GetEnumerator() {
179 return ((IEnumerable)Dictionary).GetEnumerator();
180 }
181 #endregion
182
183 #region IValueProvider Members
184 [SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes",
185 Justification = "The declaring type is obsolete, so there is little benefit to exposing this as a virtual method.")]
186 bool IValueProvider.ContainsPrefix(string prefix) {
187 if (prefix == null) {
188 throw new ArgumentNullException("prefix");
189 }
190
191 return ValueProviderUtil.CollectionContainsPrefix(Keys, prefix);
192 }
193
194 [SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes",
195 Justification = "The declaring type is obsolete, so there is little benefit to exposing this as a virtual method.")]
196 ValueProviderResult IValueProvider.GetValue(string key) {
197 if (key == null) {
198 throw new ArgumentNullException("key");
199 }
200
201 ValueProviderResult vpResult;
202 TryGetValue(key, out vpResult);
203 return vpResult;
204 }
205 #endregion
206
207 }
208}