Exemple #1
0
        /// <inheritdoc/>
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            var model = (ActionExecutionModel)bindingContext.Model;

            var metadataProvider        = bindingContext.HttpContext.RequestServices.GetService <IModelMetadataProvider>();
            var metadataDetailsProvider = bindingContext.HttpContext.RequestServices.GetService <ICompositeMetadataDetailsProvider>();
            var loggerFactory           = bindingContext.HttpContext.RequestServices.GetService <ILoggerFactory>();

            bindingContext.ModelMetadata = ActionExecutionModelMetadataFactory.GetMetadata(
                metadataProvider,
                metadataDetailsProvider,
                model);

            var propertyBinders = new Dictionary <ModelMetadata, IModelBinder>(bindingContext.ModelMetadata.Properties.Count);

            foreach (var propertyMetadata in bindingContext.ModelMetadata.Properties)
            {
                propertyBinders[propertyMetadata] = providerContext.CreateBinder(propertyMetadata);
            }

#pragma warning disable CS0618 // Type or member is obsolete
            var modelBinder = new ComplexTypeModelBinder(propertyBinders, loggerFactory);
#pragma warning restore CS0618 // Type or member is obsolete

            await modelBinder.BindModelAsync(bindingContext);
        }
            /// <inheritdoc/>
            public IEnumerable <ModelMetadata> GetMetadataForProperties(Type modelType)
            {
                if (actionExecutionModel != null &&
                    typeof(ActionExecutionModel).IsAssignableFrom(modelType) &&
                    originalModelMetadataProvider is not ActionExecutionModelMetadataProviderScope)
                {
                    var compositeMetadataDetailsProvider = controller.HttpContext.RequestServices.GetService <ICompositeMetadataDetailsProvider>();

                    var metadata = ActionExecutionModelMetadataFactory.GetMetadata(originalModelMetadataProvider, compositeMetadataDetailsProvider, actionExecutionModel);

                    return(metadata.GetMetadataForProperties(modelType));
                }
                else
                {
                    return(originalModelMetadataProvider.GetMetadataForProperties(modelType));
                }
            }
            /// <inheritdoc/>
            public ModelMetadata GetMetadataForType(Type modelType)
            {
                if (modelType == null)
                {
                    throw new ArgumentNullException(nameof(modelType));
                }

                if (actionExecutionModel != null &&
                    typeof(ActionExecutionModel).IsAssignableFrom(modelType) &&
                    originalModelMetadataProvider is not ActionExecutionModelMetadataProviderScope)
                {
                    var compositeMetadataDetailsProvider = controller.HttpContext.RequestServices.GetService <ICompositeMetadataDetailsProvider>();

                    return(ActionExecutionModelMetadataFactory.GetMetadata(originalModelMetadataProvider, compositeMetadataDetailsProvider, actionExecutionModel));
                }
                else
                {
                    return(originalModelMetadataProvider.GetMetadataForType(modelType));
                }
            }