Ejemplo n.º 1
0
        public async Task <IDictionary <string, object> > GetActionArgumentsAsync(ActionContext actionContext)
        {
            var actionBindingContext = await _bindingContextProvider.GetActionBindingContextAsync(actionContext);

            var metadataProvider = actionBindingContext.MetadataProvider;

            var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor;

            if (actionDescriptor == null)
            {
                throw new ArgumentException(
                          Resources.FormatActionDescriptorMustBeBasedOnControllerAction(
                              typeof(ControllerActionDescriptor)),
                          nameof(actionContext));
            }

            var parameterMetadata = new List <ModelMetadata>();

            foreach (var parameter in actionDescriptor.Parameters)
            {
                var metadata = metadataProvider.GetMetadataForParameter(
                    modelAccessor: null,
                    methodInfo: actionDescriptor.MethodInfo,
                    parameterName: parameter.Name,
                    binderMetadata: parameter.BinderMetadata);

                if (metadata != null)
                {
                    parameterMetadata.Add(metadata);
                }
            }

            var bodyBoundParameterCount = parameterMetadata.Count(
                modelMetadata => modelMetadata.BinderMetadata is IFormatterBinderMetadata);

            if (bodyBoundParameterCount > 1)
            {
                throw new InvalidOperationException(Resources.MultipleBodyParametersAreNotAllowed);
            }

            var actionArguments = new Dictionary <string, object>(StringComparer.Ordinal);

            foreach (var parameter in parameterMetadata)
            {
                await PopulateArgumentAsync(actionBindingContext, actionArguments, parameter);
            }

            return(actionArguments);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public IEnumerable <ModelClientValidationRule> GetClientValidationRules(
            [NotNull] ViewContext viewContext,
            ModelMetadata metadata,
            string name)
        {
            var actionBindingContext = _actionBindingContextProvider.GetActionBindingContextAsync(viewContext).Result;

            metadata = metadata ??
                       ExpressionMetadataProvider.FromStringExpression(name, viewContext.ViewData, _metadataProvider);

            return(actionBindingContext
                   .ValidatorProvider
                   .GetValidators(metadata)
                   .OfType <IClientModelValidator>()
                   .SelectMany(v => v.GetClientValidationRules(
                                   new ClientModelValidationContext(metadata, _metadataProvider))));
        }
        public async Task <IDictionary <string, object> > GetActionArgumentsAsync(ActionContext actionContext)
        {
            var actionBindingContext = await _bindingContextProvider.GetActionBindingContextAsync(actionContext);

            var metadataProvider = actionBindingContext.MetadataProvider;

            var actionDescriptor = actionContext.ActionDescriptor as ControllerActionDescriptor;

            if (actionDescriptor == null)
            {
                throw new ArgumentException(
                          Resources.FormatActionDescriptorMustBeBasedOnControllerAction(
                              typeof(ControllerActionDescriptor)),
                          nameof(actionContext));
            }

            var parameterMetadata = new List <ModelMetadata>();

            foreach (var parameter in actionDescriptor.Parameters)
            {
                var metadata = metadataProvider.GetMetadataForParameter(
                    modelAccessor: null,
                    methodInfo: actionDescriptor.MethodInfo,
                    parameterName: parameter.Name);

                if (metadata != null)
                {
                    UpdateParameterMetadata(metadata, parameter.BinderMetadata);
                    parameterMetadata.Add(metadata);
                }
            }

            var actionArguments = new Dictionary <string, object>(StringComparer.Ordinal);

            await PopulateArgumentAsync(actionBindingContext, actionArguments, parameterMetadata);

            return(actionArguments);
        }