Exemple #1
0
        /// <inheritdoc />
        public async Task <ModelBindingResult> BindModelAsync([NotNull] ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType == typeof(IFormFile))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                var value          = postedFiles.FirstOrDefault();
                var validationNode =
                    new ModelValidationNode(bindingContext.ModelName, bindingContext.ModelMetadata, value);
                return(new ModelBindingResult(
                           value,
                           bindingContext.ModelName,
                           isModelSet: value != null,
                           validationNode: validationNode));
            }
            else if (typeof(IEnumerable <IFormFile>).GetTypeInfo().IsAssignableFrom(
                         bindingContext.ModelType.GetTypeInfo()))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                var value          = ModelBindingHelper.ConvertValuesToCollectionType(bindingContext.ModelType, postedFiles);
                var validationNode =
                    new ModelValidationNode(bindingContext.ModelName, bindingContext.ModelMetadata, value);
                return(new ModelBindingResult(
                           value,
                           bindingContext.ModelName,
                           isModelSet: value != null,
                           validationNode: validationNode));
            }

            return(null);
        }
Exemple #2
0
        /// <inheritdoc />
        protected override Task <bool> BindAsync(
            [NotNull] ModelBindingContext bindingContext,
            [NotNull] IHeaderBinderMetadata metadata)
        {
            var request = bindingContext.OperationBindingContext.HttpContext.Request;

            if (bindingContext.ModelType == typeof(string))
            {
                var value = request.Headers.Get(bindingContext.ModelName);
                if (value != null)
                {
                    bindingContext.Model = value;
                }

                return(Task.FromResult(true));
            }
            else if (typeof(IEnumerable <string>).GetTypeInfo().IsAssignableFrom(
                         bindingContext.ModelType.GetTypeInfo()))
            {
                var values = request.Headers.GetCommaSeparatedValues(bindingContext.ModelName);
                if (values != null)
                {
                    bindingContext.Model = ModelBindingHelper.ConvertValuesToCollectionType(bindingContext.ModelType, values);
                }

                return(Task.FromResult(true));
            }

            return(Task.FromResult(false));
        }
        /// <inheritdoc />
        protected override Task <ModelBindingResult> BindModelCoreAsync([NotNull] ModelBindingContext bindingContext)
        {
            var request       = bindingContext.OperationBindingContext.HttpContext.Request;
            var modelMetadata = bindingContext.ModelMetadata;

            // Property name can be null if the model metadata represents a type (rather than a property or parameter).
            var    headerName = bindingContext.BinderModelName ?? modelMetadata.PropertyName ?? bindingContext.ModelName;
            object model      = null;

            if (bindingContext.ModelType == typeof(string))
            {
                var value = request.Headers.Get(headerName);
                if (value != null)
                {
                    model = value;
                }
            }
            else if (typeof(IEnumerable <string>).GetTypeInfo().IsAssignableFrom(
                         bindingContext.ModelType.GetTypeInfo()))
            {
                var values = request.Headers.GetCommaSeparatedValues(headerName);
                if (values != null)
                {
                    model = ModelBindingHelper.ConvertValuesToCollectionType(
                        bindingContext.ModelType,
                        values);
                }
            }

            return(Task.FromResult(new ModelBindingResult(model, bindingContext.ModelName, isModelSet: model != null)));
        }
Exemple #4
0
        /// <inheritdoc />
        public async Task <bool> BindModelAsync([NotNull] ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType == typeof(IFormFile))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                var value = postedFiles.FirstOrDefault();
                if (value != null)
                {
                    bindingContext.Model = value;
                }

                return(true);
            }
            else if (typeof(IEnumerable <IFormFile>).GetTypeInfo().IsAssignableFrom(
                         bindingContext.ModelType.GetTypeInfo()))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                var value = ModelBindingHelper.ConvertValuesToCollectionType(bindingContext.ModelType, postedFiles);
                if (value != null)
                {
                    bindingContext.Model = value;
                }

                return(true);
            }

            return(false);
        }
Exemple #5
0
        /// <inheritdoc />
        public Task <ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
        {
            // This method is optimized to use cached tasks when possible and avoid allocating
            // using Task.FromResult. If you need to make changes of this nature, profile
            // allocations afterwards and look for Task<ModelBindingResult>.

            var allowedBindingSource = bindingContext.BindingSource;

            if (allowedBindingSource == null ||
                !allowedBindingSource.CanAcceptDataFrom(BindingSource.Header))
            {
                // Headers are opt-in. This model either didn't specify [FromHeader] or specified something
                // incompatible so let other binders run.
                return(ModelBindingResult.NoResultAsync);
            }

            var request       = bindingContext.OperationBindingContext.HttpContext.Request;
            var modelMetadata = bindingContext.ModelMetadata;

            // Property name can be null if the model metadata represents a type (rather than a property or parameter).
            var    headerName = bindingContext.FieldName;
            object model      = null;

            if (bindingContext.ModelType == typeof(string))
            {
                string value = request.Headers[headerName];
                if (value != null)
                {
                    model = value;
                }
            }
            else if (typeof(IEnumerable <string>).IsAssignableFrom(bindingContext.ModelType))
            {
                var values = request.Headers.GetCommaSeparatedValues(headerName);
                if (values.Length > 0)
                {
                    model = ModelBindingHelper.ConvertValuesToCollectionType(
                        bindingContext.ModelType,
                        values);
                }
            }

            if (model == null)
            {
                return(ModelBindingResult.FailedAsync(bindingContext.ModelName));
            }
            else
            {
                bindingContext.ModelState.SetModelValue(
                    bindingContext.ModelName,
                    request.Headers.GetCommaSeparatedValues(headerName),
                    request.Headers[headerName]);

                return(ModelBindingResult.SuccessAsync(bindingContext.ModelName, model));
            }
        }
        /// <inheritdoc />
        public async Task <ModelBindingResult> BindModelAsync([NotNull] ModelBindingContext bindingContext)
        {
            object value;

            if (bindingContext.ModelType == typeof(IFormFile))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                value = postedFiles.FirstOrDefault();
            }
            else if (typeof(IEnumerable <IFormFile>).IsAssignableFrom(bindingContext.ModelType))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                value = ModelBindingHelper.ConvertValuesToCollectionType(bindingContext.ModelType, postedFiles);
            }
            else
            {
                // This binder does not support the requested type.
                return(null);
            }

            ModelValidationNode validationNode = null;

            if (value != null)
            {
                validationNode =
                    new ModelValidationNode(bindingContext.ModelName, bindingContext.ModelMetadata, value)
                {
                    SuppressValidation = true,
                };

                var valueProviderResult = new ValueProviderResult(rawValue: value);
                bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
            }

            return(new ModelBindingResult(
                       value,
                       bindingContext.ModelName,
                       isModelSet: value != null,
                       validationNode: validationNode));
        }
Exemple #7
0
        private async Task <ModelBindingResult> BindModelCoreAsync(ModelBindingContext bindingContext)
        {
            object value;

            if (bindingContext.ModelType == typeof(IFormFile))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                value = postedFiles.FirstOrDefault();
            }
            else if (typeof(IEnumerable <IFormFile>).IsAssignableFrom(bindingContext.ModelType))
            {
                var postedFiles = await GetFormFilesAsync(bindingContext);

                value = ModelBindingHelper.ConvertValuesToCollectionType(bindingContext.ModelType, postedFiles);
            }
            else
            {
                // This binder does not support the requested type.
                Debug.Fail("We shouldn't be called without a matching type.");
                return(ModelBindingResult.NoResult);
            }

            if (value == null)
            {
                return(ModelBindingResult.Failed(bindingContext.ModelName));
            }
            else
            {
                bindingContext.ValidationState.Add(value, new ValidationStateEntry()
                {
                    SuppressValidation = true
                });
                bindingContext.ModelState.SetModelValue(
                    bindingContext.ModelName,
                    rawValue: null,
                    attemptedValue: null);

                return(ModelBindingResult.Success(bindingContext.ModelName, value));
            }
        }