1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\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
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
27 public FormCollection() {
\r
30 public FormCollection(NameValueCollection collection) {
\r
31 if (collection == null) {
\r
32 throw new ArgumentNullException("collection");
\r
38 public ValueProviderResult GetValue(string name) {
\r
40 throw new ArgumentNullException("name");
\r
43 string[] rawValue = GetValues(name);
\r
44 if (rawValue == null) {
\r
48 string attemptedValue = this[name];
\r
49 return new ValueProviderResult(rawValue, attemptedValue, CultureInfo.CurrentCulture);
\r
52 public IValueProvider ToValueProvider() {
\r
56 #region IValueProvider Members
\r
57 bool IValueProvider.ContainsPrefix(string prefix) {
\r
58 return ValueProviderUtil.CollectionContainsPrefix(AllKeys, prefix);
\r
61 ValueProviderResult IValueProvider.GetValue(string key) {
\r
62 return GetValue(key);
\r
66 private sealed class FormCollectionBinderAttribute : CustomModelBinderAttribute {
\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
72 public override IModelBinder GetBinder() {
\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
83 return new FormCollection(controllerContext.HttpContext.Request.Form);
\r