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