New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / 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.Specialized;\r
16     using System.Diagnostics.CodeAnalysis;\r
17     using System.Globalization;\r
18     using System.Web.Mvc.Resources;\r
19 \r
20     [SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable",\r
21         Justification = "It is not anticipated that users will need to serialize this type.")]\r
22     [SuppressMessage("Microsoft.Design", "CA1035:ICollectionImplementationsHaveStronglyTypedMembers",\r
23         Justification = "It is not anticipated that users will call FormCollection.CopyTo().")]\r
24     [FormCollectionBinder]\r
25     public sealed class FormCollection : NameValueCollection, IValueProvider {\r
26 \r
27         public FormCollection() {\r
28         }\r
29 \r
30         public FormCollection(NameValueCollection collection) {\r
31             if (collection == null) {\r
32                 throw new ArgumentNullException("collection");\r
33             }\r
34 \r
35             Add(collection);\r
36         }\r
37 \r
38         public ValueProviderResult GetValue(string name) {\r
39             if (name == null) {\r
40                 throw new ArgumentNullException("name");\r
41             }\r
42 \r
43             string[] rawValue = GetValues(name);\r
44             if (rawValue == null) {\r
45                 return null;\r
46             }\r
47 \r
48             string attemptedValue = this[name];\r
49             return new ValueProviderResult(rawValue, attemptedValue, CultureInfo.CurrentCulture);\r
50         }\r
51 \r
52         public IValueProvider ToValueProvider() {\r
53             return this;\r
54         }\r
55 \r
56         #region IValueProvider Members\r
57         bool IValueProvider.ContainsPrefix(string prefix) {\r
58             return ValueProviderUtil.CollectionContainsPrefix(AllKeys, prefix);\r
59         }\r
60 \r
61         ValueProviderResult IValueProvider.GetValue(string key) {\r
62             return GetValue(key);\r
63         }\r
64         #endregion\r
65 \r
66         private sealed class FormCollectionBinderAttribute : CustomModelBinderAttribute {\r
67 \r
68             // since the FormCollectionModelBinder.BindModel() method is thread-safe, we only need to keep\r
69             // a single instance of the binder around\r
70             private static readonly FormCollectionModelBinder _binder = new FormCollectionModelBinder();\r
71 \r
72             public override IModelBinder GetBinder() {\r
73                 return _binder;\r
74             }\r
75 \r
76             // this class is used for generating a FormCollection object\r
77             private sealed class FormCollectionModelBinder : IModelBinder {\r
78                 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {\r
79                     if (controllerContext == null) {\r
80                         throw new ArgumentNullException("controllerContext");\r
81                     }\r
82 \r
83                     return new FormCollection(controllerContext.HttpContext.Request.Form);\r
84                 }\r
85             }\r
86         }\r
87 \r
88     }\r
89 }\r