예제 #1
0
        public async Task <MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
        {
            context.ThrowIfNull("context");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            parameter.ThrowIfNull("parameter");

            Type   parameterType = parameter.ParameterType;
            object model         = _container != null?_container.GetInstance(parameterType) : Activator.CreateInstance(parameterType);

            Type modelType = model.GetType();

            foreach (PropertyInfo property in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                object mappedValue = await GetMappedValueAsync(context, modelType, property);

                if (mappedValue == null)
                {
                    throw new ApplicationException(String.Format("Unable to map property '{0} {1}' of type '{2}'.", property.PropertyType.FullName, property.Name, modelType.FullName));
                }

                property.SetValue(model, mappedValue, null);
            }

            return(MapResult.ValueMapped(model));
        }
예제 #2
0
        public Task <MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
        {
            context.ThrowIfNull("request");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            parameter.ThrowIfNull("parameter");

            Type   parameterType = parameter.ParameterType;
            var    reader        = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
            string json          = reader.ReadToEnd();
            object jsonModel;

            try
            {
                jsonModel = JsonConvert.DeserializeObject(json, parameterType, _serializerSettings);
            }
            catch (Exception exception)
            {
                if (_errorHandling == DataConversionErrorHandling.ThrowException)
                {
                    throw new ApplicationException(String.Format("Request content could not be deserialized to '{0}'.", parameterType.FullName), exception);
                }
                jsonModel = parameterType.GetDefaultValue();
            }

            return(MapResult.ValueMapped(jsonModel).AsCompletedTask());
        }
예제 #3
0
        public Task <MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
        {
            context.ThrowIfNull("context");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            parameter.ThrowIfNull("parameter");

            return(MapResult.ValueMapped(parameter.ParameterType.GetDefaultValue()).AsCompletedTask());
        }
예제 #4
0
        private async Task <object> GetMappedValueAsync(HttpContextBase context, Type modelType, PropertyInfo property)
        {
            object mappedValue = null;

            foreach (IModelPropertyMapper modelPropertyMapper in _modelPropertyMappers)
            {
                MapResult result = await modelPropertyMapper.MapAsync(context.Request, modelType, property);

                if (result.ResultType == MapResultType.ValueMapped)
                {
                    mappedValue = result.Value;
                    break;
                }
            }
            return(mappedValue);
        }
        public Task <MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
        {
            context.ThrowIfNull("context");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            parameter.ThrowIfNull("parameter");

            Type   parameterType = parameter.ParameterType;
            string parameterName = parameter.Name;
            string field         = context.Request.Form.AllKeys.LastOrDefault(arg => String.Equals(arg, parameterName, _caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase));

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

            IConvertible value = context.Request.Form[field];
            object       convertedValue;

            try
            {
                convertedValue = value.ToType(parameterType, CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                if (_errorHandling == DataConversionErrorHandling.ThrowException)
                {
                    throw new ApplicationException(
                              String.Format(
                                  "Value for form field '{0}' could not be converted to parameter '{1} {2}' of {3}.{4}.",
                                  field,
                                  parameterType.FullName,
                                  parameterName,
                                  type.FullName,
                                  method.Name),
                              exception);
                }
                convertedValue = parameterType.GetDefaultValue();
            }

            return(MapResult.ValueMapped(convertedValue).AsCompletedTask());
        }
예제 #6
0
 public void SetUp()
 {
     _result = MapResult.ValueNotMapped();
 }
예제 #7
0
 public void SetUp()
 {
     _result = MapResult.ValueMapped(0);
 }