public TokenPassThroughProviderFactory(
     string token, 
     string contentType, 
     JsonValueProviderFactory existing)
 {
     _token = token;
     _contentType = contentType;
     _existing = existing;
 }
Ejemplo n.º 2
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var actionName = (string)controllerContext.RouteData.Values["action"];
            var mapping = GetMethodMapping(controllerContext, bindingContext, actionName);

            var method = new Method(new MethodDescriptor(mapping, controllerContext.GetActionDescriptor(actionName)));

            foreach (var parameter in method.Parameters)
            {
                ValueProviderResult result;
                if (parameter.IsModelCollection)
                {
                    var type = typeof(List<>).MakeGenericType(parameter.UnderliningModel.ModelType);
                    var collection = Activator.CreateInstance(type);
                    var index = 0;
                    while ((result = bindingContext.ValueProvider.GetValue(string.Format("{0}[{1}]", parameter.Name, index))) != null)
                    {
                        var item = ModelBinder.GetModelValue(result, parameter.UnderliningModel.ModelType) ?? Activator.CreateInstance(parameter.UnderliningModel.ModelType);
                        var itemModel = new Model(parameter.UnderliningModel.ModelType, parameter.UnderliningModel.Descriptor, item);
                        foreach (var itemProperty in itemModel.Properties)
                        {
                            var itemPropertyName = string.Format("{0}[{1}].{2}", parameter.Name, index, itemProperty.Name);
                            var itemResult = bindingContext.ValueProvider.GetValue(itemPropertyName);
                            if (itemResult != null)
                            {
                                itemProperty.Value = !itemProperty.IsModel
                                    ? ModelBinder.GetNonModelValue(itemResult, itemProperty.MemberType)
                                    : ModelBinder.GetModelValue(itemResult, itemProperty.MemberType);
                            }
                        }
                        type.GetMethod("Add").Invoke(collection, new[] { item });
                        index++;
                    }
                    parameter.Value = collection;
                }
                else
                {
                    result = bindingContext.ValueProvider.GetValue(parameter.Name);

                    if (result == null)
                    {
                        controllerContext.HttpContext.Request.InputStream.Position = 0;
                        var provider = new JsonValueProviderFactory().GetValueProvider(controllerContext);
                        if (provider != null)
                            result = provider.GetValue(parameter.Name);
                    }

                    if (result != null)
                    {
                        parameter.Value = !parameter.IsModel
                            ? ModelBinder.GetNonModelValue(result, parameter.MemberType)
                            : ModelBinder.GetModelValue(result, parameter.MemberType);
                    }
                }
            }
            return method;
        }
        public override IValueProvider GetValueProvider(ControllerContext controllerContext)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException(nameof(controllerContext));
            }

            if (controllerContext.HttpContext.Request.FilePath != "/Match/Callback")
            {
                // Fallback
                var prov = new JsonValueProviderFactory();
                return(prov.GetValueProvider(controllerContext));
            }

            if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var reader   = new StreamReader(controllerContext.HttpContext.Request.InputStream);
            var bodyText = reader.ReadToEnd();

            if (string.IsNullOrEmpty(bodyText))
            {
                return(null);
            }

            CallbackResult jsonObject = null;

            try
            {
                jsonObject = JsonConvert.DeserializeObject <CallbackResult>(bodyText);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return
                (new DictionaryValueProvider <CallbackResult>(
                     new Dictionary <string, CallbackResult> {
                { "obj", jsonObject }
            }, CultureInfo.CurrentCulture));
        }