Exemplo n.º 1
0
		public Task<MapResult> MapAsync(HttpRequestBase request, Type modelType, PropertyInfo property)
		{
			request.ThrowIfNull("request");
			modelType.ThrowIfNull("modelType");
			property.ThrowIfNull("property");

			return MapResult.ValueMapped(property.PropertyType.GetDefaultValue()).AsCompletedTask();
		}
Exemplo n.º 2
0
        public MapResult Map(HttpRequestBase request, Type modelType, PropertyInfo property)
        {
            request.ThrowIfNull("request");
            modelType.ThrowIfNull("modelType");
            property.ThrowIfNull("property");

            return MapResult.ValueMapped(property.PropertyType.GetDefaultValue());
        }
Exemplo n.º 3
0
        public Task<MapResult> MapAsync(HttpContextBase context, Type modelType, PropertyInfo property)
        {
            context.ThrowIfNull("context");
            modelType.ThrowIfNull("modelType");
            property.ThrowIfNull("property");

            Type propertyType = property.PropertyType;
            string propertyName = property.Name;
            NameValueCollection nameValueCollection;

            switch (_source)
            {
                case NameValueCollectionSource.Form:
                    nameValueCollection = context.Request.Form;
                    break;
                case NameValueCollectionSource.QueryString:
                    nameValueCollection = context.Request.QueryString;
                    break;
                default:
                    throw new InvalidOperationException(String.Format("Unexpected name-value collection source {0}.", _source));
            }
            string field = nameValueCollection.AllKeys.LastOrDefault(arg => String.Equals(arg, propertyName, _caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase));

            if (field == null)
            {
                return MapResult.ValueNotMapped().AsCompletedTask();
            }

            string value = nameValueCollection[field];

            try
            {
                return OnMapAsync(context, value, propertyType);
            }
            catch (Exception exception)
            {
                if (_errorHandling == DataConversionErrorHandling.ThrowException)
                {
                    throw new ApplicationException(
                        String.Format(
                            "Value of form field '{0}' could not be converted to property '{1} {2}' of type '{3}'.",
                            field,
                            propertyType.FullName,
                            propertyName,
                            modelType.FullName),
                        exception);
                }

                return MapResult.ValueMapped(propertyType.GetDefaultValue()).AsCompletedTask();
            }
        }
		public Task<MapResult> MapAsync(HttpRequestBase request, Type modelType, PropertyInfo property)
		{
			request.ThrowIfNull("request");
			modelType.ThrowIfNull("modelType");
			property.ThrowIfNull("property");

			Type propertyType = property.PropertyType;
			string propertyName = property.Name;
			string field = request.QueryString.AllKeys.LastOrDefault(arg => String.Equals(arg, propertyName, _caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase));

			if (field == null)
			{
				return MapResult.ValueNotMapped().AsCompletedTask();
			}

			IConvertible value = request.QueryString[field];
			object convertedValue;

			try
			{
				convertedValue = value.ToType(propertyType, CultureInfo.InvariantCulture);
			}
			catch (Exception exception)
			{
				if (_errorHandling == DataConversionErrorHandling.ThrowException)
				{
					throw new ApplicationException(
						String.Format(
							"Value of query string field '{0}' could not be converted to property '{1} {2}' of type '{3}'.",
							field,
							propertyType.FullName,
							propertyName,
							modelType.FullName),
						exception);
				}
				convertedValue = propertyType.GetDefaultValue();
			}

			return MapResult.ValueMapped(convertedValue).AsCompletedTask();
		}