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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
namespace System.Web.Mvc {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web.Routing;
[Obsolete("The recommended alternative is to use one of the specific ValueProvider types, such as FormValueProvider.")]
public class ValueProviderDictionary : IDictionary<string, ValueProviderResult>, IValueProvider {
private readonly Dictionary<string, ValueProviderResult> _dictionary = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);
public ValueProviderDictionary(ControllerContext controllerContext) {
ControllerContext = controllerContext;
if (controllerContext != null) {
PopulateDictionary();
}
}
public ControllerContext ControllerContext {
get;
private set;
}
public int Count {
get {
return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Count;
}
}
internal Dictionary<string, ValueProviderResult> Dictionary {
get {
return _dictionary;
}
}
public bool IsReadOnly {
get {
return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).IsReadOnly;
}
}
public ICollection<string> Keys {
get {
return Dictionary.Keys;
}
}
public ValueProviderResult this[string key] {
get {
ValueProviderResult result;
Dictionary.TryGetValue(key, out result);
return result;
}
set {
Dictionary[key] = value;
}
}
public ICollection<ValueProviderResult> Values {
get {
return Dictionary.Values;
}
}
public void Add(KeyValuePair<string, ValueProviderResult> item) {
((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Add(item);
}
public void Add(string key, object value) {
string attemptedValue = Convert.ToString(value, CultureInfo.InvariantCulture);
ValueProviderResult vpResult = new ValueProviderResult(value, attemptedValue, CultureInfo.InvariantCulture);
Add(key, vpResult);
}
public void Add(string key, ValueProviderResult value) {
Dictionary.Add(key, value);
}
private void AddToDictionaryIfNotPresent(string key, ValueProviderResult result) {
if (!String.IsNullOrEmpty(key)) {
if (!Dictionary.ContainsKey(key)) {
Dictionary.Add(key, result);
}
}
}
public void Clear() {
((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Clear();
}
public bool Contains(KeyValuePair<string, ValueProviderResult> item) {
return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Contains(item);
}
public bool ContainsKey(string key) {
return Dictionary.ContainsKey(key);
}
public void CopyTo(KeyValuePair<string, ValueProviderResult>[] array, int arrayIndex) {
((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).CopyTo(array, arrayIndex);
}
public IEnumerator<KeyValuePair<string, ValueProviderResult>> GetEnumerator() {
return ((IEnumerable<KeyValuePair<string, ValueProviderResult>>)Dictionary).GetEnumerator();
}
private void PopulateDictionary() {
CultureInfo currentCulture = CultureInfo.CurrentCulture;
CultureInfo invariantCulture = CultureInfo.InvariantCulture;
// We use this order of precedence to populate the dictionary:
// 1. Request form submission (should be culture-aware)
// 2. Values from the RouteData (could be from the typed-in URL or from the route's default values)
// 3. URI query string
NameValueCollection form = ControllerContext.HttpContext.Request.Form;
if (form != null) {
string[] keys = form.AllKeys;
foreach (string key in keys) {
string[] rawValue = form.GetValues(key);
string attemptedValue = form[key];
ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, currentCulture);
AddToDictionaryIfNotPresent(key, result);
}
}
RouteValueDictionary routeValues = ControllerContext.RouteData.Values;
if (routeValues != null) {
foreach (var kvp in routeValues) {
string key = kvp.Key;
object rawValue = kvp.Value;
string attemptedValue = Convert.ToString(rawValue, invariantCulture);
ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
AddToDictionaryIfNotPresent(key, result);
}
}
NameValueCollection queryString = ControllerContext.HttpContext.Request.QueryString;
if (queryString != null) {
string[] keys = queryString.AllKeys;
foreach (string key in keys) {
string[] rawValue = queryString.GetValues(key);
string attemptedValue = queryString[key];
ValueProviderResult result = new ValueProviderResult(rawValue, attemptedValue, invariantCulture);
AddToDictionaryIfNotPresent(key, result);
}
}
}
public bool Remove(KeyValuePair<string, ValueProviderResult> item) {
return ((ICollection<KeyValuePair<string, ValueProviderResult>>)Dictionary).Remove(item);
}
public bool Remove(string key) {
return Dictionary.Remove(key);
}
public bool TryGetValue(string key, out ValueProviderResult value) {
return Dictionary.TryGetValue(key, out value);
}
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable)Dictionary).GetEnumerator();
}
#endregion
#region IValueProvider Members
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "The declaring type is obsolete, so there is little benefit to exposing this as a virtual method.")]
bool IValueProvider.ContainsPrefix(string prefix) {
if (prefix == null) {
throw new ArgumentNullException("prefix");
}
return ValueProviderUtil.CollectionContainsPrefix(Keys, prefix);
}
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "The declaring type is obsolete, so there is little benefit to exposing this as a virtual method.")]
ValueProviderResult IValueProvider.GetValue(string key) {
if (key == null) {
throw new ArgumentNullException("key");
}
ValueProviderResult vpResult;
TryGetValue(key, out vpResult);
return vpResult;
}
#endregion
}
}
|