Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / FormCollection.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Collections.Specialized;\r
17     using System.Diagnostics.CodeAnalysis;\r
18     using System.Globalization;\r
19     using System.Web.Mvc.Resources;\r
20 \r
21     [SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable",\r
22         Justification = "It is not anticipated that users will need to serialize this type.")]\r
23     [SuppressMessage("Microsoft.Design", "CA1035:ICollectionImplementationsHaveStronglyTypedMembers",\r
24         Justification = "It is not anticipated that users will call FormCollection.CopyTo().")]\r
25     [FormCollectionBinder]\r
26     public class FormCollection : NameValueCollection {\r
27 \r
28         public FormCollection() {\r
29         }\r
30 \r
31         public FormCollection(NameValueCollection collection) {\r
32             if (collection == null) {\r
33                 throw new ArgumentNullException("collection");\r
34             }\r
35 \r
36             Add(collection);\r
37         }\r
38 \r
39         public IDictionary<string, ValueProviderResult> ToValueProvider() {\r
40             CultureInfo currentCulture = CultureInfo.CurrentCulture;\r
41 \r
42             Dictionary<string, ValueProviderResult> dict = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);\r
43             string[] keys = AllKeys;\r
44             foreach (string key in keys) {\r
45                 string[] rawValue = GetValues(key);\r
46                 string attemptedValue = this[key];\r
47                 ValueProviderResult vpResult = new ValueProviderResult(rawValue, attemptedValue, currentCulture);\r
48                 dict[key] = vpResult;\r
49             }\r
50 \r
51             return dict;\r
52         }\r
53 \r
54         public virtual ValueProviderResult GetValue(string name) {\r
55             if (String.IsNullOrEmpty(name)) {\r
56                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");\r
57             }\r
58 \r
59             string[] rawValue = GetValues(name);\r
60             if (rawValue == null) {\r
61                 return null;\r
62             }\r
63 \r
64             string attemptedValue = this[name];\r
65             return new ValueProviderResult(rawValue, attemptedValue, CultureInfo.CurrentCulture);\r
66         }\r
67 \r
68         private sealed class FormCollectionBinderAttribute : CustomModelBinderAttribute {\r
69 \r
70             // since the FormCollectionModelBinder.BindModel() method is thread-safe, we only need to keep\r
71             // a single instance of the binder around\r
72             private static readonly FormCollectionModelBinder _binder = new FormCollectionModelBinder();\r
73 \r
74             public override IModelBinder GetBinder() {\r
75                 return _binder;\r
76             }\r
77 \r
78             // this class is used for generating a FormCollection object\r
79             private sealed class FormCollectionModelBinder : IModelBinder {\r
80                 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {\r
81                     if (controllerContext == null) {\r
82                         throw new ArgumentNullException("controllerContext");\r
83                     }\r
84 \r
85                     return new FormCollection(controllerContext.HttpContext.Request.Form);\r
86                 }\r
87             }\r
88         }\r
89 \r
90     }\r
91 }\r