private static CompositeValueProvider CreateDefaultValueProvider() { var result = new CompositeValueProvider(); result.Add(new RouteValueProvider(BindingSource.Path, new RouteValueDictionary())); result.Add(new QueryStringValueProvider( BindingSource.Query, new QueryCollection(), CultureInfo.InvariantCulture)); result.Add(new FormValueProvider( BindingSource.Form, new FormCollection(new Dictionary <string, StringValues>()), CultureInfo.CurrentCulture)); return(result); }
private static CompositeValueProvider CreateDefaultValueProvider() { var result = new CompositeValueProvider(); result.Add(new RouteValueProvider(BindingSource.Path, new RouteValueDictionary())); result.Add(new QueryStringValueProvider( BindingSource.Query, new QueryCollection(), CultureInfo.InvariantCulture)); result.Add(new FormValueProvider( BindingSource.Form, new FormCollection(new Dictionary<string, StringValues>()), CultureInfo.CurrentCulture)); return result; }
private Task <object> BindInternal(HttpContext context, Type returnType) { var mediatorRequest = Activator.CreateInstance(returnType); var requestProperties = typeBindingRepo.GetProperties(returnType); if (!requestProperties.Any()) { return(Task.FromResult(mediatorRequest)); } var routeProvider = new RouteValueProvider(BindingSource.Path, context.Request.RouteValues); var queryProvider = new QueryStringValueProvider(BindingSource.Query, context.Request.Query, CultureInfo.InvariantCulture); var compositeValueProvider = new CompositeValueProvider { routeProvider, queryProvider }; IValueProvider formProvider = null; var bodyProvider = new JsonBodyValueProvider(context, jsonOptions); if (context.Request.HasFormContentType) { formProvider = new FormValueProvider(BindingSource.Form, context.Request.Form, CultureInfo.CurrentCulture);; compositeValueProvider.Add(formProvider); } else { compositeValueProvider.Add(bodyProvider); } IValueProvider GetProvider(BoundProperty property) { if (property.BindingSource == BindingSource.Path) { return(routeProvider); } else if (property.BindingSource == BindingSource.Query) { return(queryProvider); } else if (property.BindingSource == BindingSource.Form) { return(formProvider); } else if (property.BindingSource == BindingSource.Body) { return(bodyProvider); } return(compositeValueProvider); } foreach (var property in requestProperties) { var propType = property.Property.PropertyType; var valueProvider = GetProvider(property); if (valueProvider != null) { var value = valueProvider.GetValue(property.Name); if (value.FirstValue != null) { if (Nullable.GetUnderlyingType(propType) != null) { propType = Nullable.GetUnderlyingType(propType); } if (propType == typeof(string)) { property.Property.SetValue(mediatorRequest, value.FirstValue); } else if (propType == typeof(bool)) { property.Property.SetValue(mediatorRequest, Convert.ToBoolean(value.FirstValue)); } else if (propType == typeof(char)) { property.Property.SetValue(mediatorRequest, Convert.ToChar(value.FirstValue)); } else if (propType == typeof(DateTime)) { property.Property.SetValue(mediatorRequest, Convert.ToDateTime(value.FirstValue)); } else if (propType == typeof(DateTimeOffset)) { property.Property.SetValue(mediatorRequest, DateTimeOffset.Parse(value.FirstValue)); } else if (propType == typeof(TimeSpan)) { property.Property.SetValue(mediatorRequest, TimeSpan.Parse(value.FirstValue)); } else if (propType == typeof(Uri)) { property.Property.SetValue(mediatorRequest, new Uri(value.FirstValue)); } else if (propType == typeof(Version)) { property.Property.SetValue(mediatorRequest, new Version(value.FirstValue)); } else if (propType == typeof(Guid)) { property.Property.SetValue(mediatorRequest, Guid.Parse(value.FirstValue)); } else if (propType == typeof(decimal) || propType == typeof(double) || propType == typeof(int) || propType == typeof(uint) || propType == typeof(byte) || propType == typeof(sbyte) || propType == typeof(long) || propType == typeof(float) || propType == typeof(short) || propType == typeof(ulong) || propType == typeof(ushort)) { var objValue = JsonSerializer.Deserialize(value.FirstValue, propType, jsonOptions.Value.JsonSerializerOptions); property.Property.SetValue(mediatorRequest, objValue); } else { var needsQuotes = bodyProvider.ValueKind == JsonValueKind.String || bodyProvider.ValueKind == null; var text = needsQuotes ? $"\"{value.FirstValue}\"" : value.FirstValue; var objValue = JsonSerializer.Deserialize(text, propType, jsonOptions.Value.JsonSerializerOptions); property.Property.SetValue(mediatorRequest, objValue); } } } } return(Task.FromResult(mediatorRequest)); }