1
/* ****************************************************************************
3 * Copyright (c) Microsoft Corporation. All rights reserved.
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.
9 * You must not remove this notice, or any other, from this software.
11 * ***************************************************************************/
13 namespace System
.Web
.Mvc
{
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
;
22 public class ValueProviderDictionary
: IDictionary
<string, ValueProviderResult
> {
24 private readonly Dictionary
<string, ValueProviderResult
> _dictionary
= new Dictionary
<string, ValueProviderResult
>(StringComparer
.OrdinalIgnoreCase
);
26 public ValueProviderDictionary(ControllerContext controllerContext
) {
27 ControllerContext
= controllerContext
;
28 if (controllerContext
!= null) {
33 public ControllerContext ControllerContext
{
38 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
41 return ((ICollection
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).Count
;
45 internal Dictionary
<string, ValueProviderResult
> Dictionary
{
51 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
52 public bool IsReadOnly
{
54 return ((ICollection
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).IsReadOnly
;
58 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
59 public ICollection
<string> Keys
{
61 return Dictionary
.Keys
;
65 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
66 public ValueProviderResult
this[string key
] {
68 ValueProviderResult result
;
69 Dictionary
.TryGetValue(key
, out result
);
73 Dictionary
[key
] = value;
77 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
78 public ICollection
<ValueProviderResult
> Values
{
80 return Dictionary
.Values
;
84 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
85 public void Add(KeyValuePair
<string, ValueProviderResult
> item
) {
86 ((ICollection
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).Add(item
);
89 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
90 public void Add(string key
, ValueProviderResult
value) {
91 Dictionary
.Add(key
, value);
94 private void AddToDictionaryIfNotPresent(string key
, ValueProviderResult result
) {
95 if (!String
.IsNullOrEmpty(key
)) {
96 if (!Dictionary
.ContainsKey(key
)) {
97 Dictionary
.Add(key
, result
);
102 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
103 public void Clear() {
104 ((ICollection
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).Clear();
107 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
108 public bool Contains(KeyValuePair
<string, ValueProviderResult
> item
) {
109 return ((ICollection
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).Contains(item
);
112 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
113 public bool ContainsKey(string key
) {
114 return Dictionary
.ContainsKey(key
);
117 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
118 public void CopyTo(KeyValuePair
<string, ValueProviderResult
>[] array
, int arrayIndex
) {
119 ((ICollection
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).CopyTo(array
, arrayIndex
);
122 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
123 public IEnumerator
<KeyValuePair
<string, ValueProviderResult
>> GetEnumerator() {
124 return ((IEnumerable
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).GetEnumerator();
127 private void PopulateDictionary() {
128 CultureInfo currentCulture
= CultureInfo
.CurrentCulture
;
129 CultureInfo invariantCulture
= CultureInfo
.InvariantCulture
;
131 // We use this order of precedence to populate the dictionary:
132 // 1. Request form submission (should be culture-aware)
133 // 2. Values from the RouteData (could be from the typed-in URL or from the route's default values)
134 // 3. URI query string
136 NameValueCollection form
= ControllerContext
.HttpContext
.Request
.Form
;
138 string[] keys
= form
.AllKeys
;
139 foreach (string key
in keys
) {
140 string[] rawValue
= form
.GetValues(key
);
141 string attemptedValue
= form
[key
];
142 ValueProviderResult result
= new ValueProviderResult(rawValue
, attemptedValue
, currentCulture
);
143 AddToDictionaryIfNotPresent(key
, result
);
147 RouteValueDictionary routeValues
= ControllerContext
.RouteData
.Values
;
148 if (routeValues
!= null) {
149 foreach (var kvp
in routeValues
) {
150 string key
= kvp
.Key
;
151 object rawValue
= kvp
.Value
;
152 string attemptedValue
= Convert
.ToString(rawValue
, invariantCulture
);
153 ValueProviderResult result
= new ValueProviderResult(rawValue
, attemptedValue
, invariantCulture
);
154 AddToDictionaryIfNotPresent(key
, result
);
158 NameValueCollection queryString
= ControllerContext
.HttpContext
.Request
.QueryString
;
159 if (queryString
!= null) {
160 string[] keys
= queryString
.AllKeys
;
161 foreach (string key
in keys
) {
162 string[] rawValue
= queryString
.GetValues(key
);
163 string attemptedValue
= queryString
[key
];
164 ValueProviderResult result
= new ValueProviderResult(rawValue
, attemptedValue
, invariantCulture
);
165 AddToDictionaryIfNotPresent(key
, result
);
170 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
171 public bool Remove(KeyValuePair
<string, ValueProviderResult
> item
) {
172 return ((ICollection
<KeyValuePair
<string, ValueProviderResult
>>)Dictionary
).Remove(item
);
175 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
176 public bool Remove(string key
) {
177 return Dictionary
.Remove(key
);
180 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
181 public bool TryGetValue(string key
, out ValueProviderResult
value) {
182 return Dictionary
.TryGetValue(key
, out value);
185 #region IEnumerable Members
186 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
187 IEnumerator IEnumerable
.GetEnumerator() {
188 return ((IEnumerable
)Dictionary
).GetEnumerator();