コード例 #1
0
        // Helper for ReadAs() to get a ModelBindingContext to invoke model binding over FormUrl data.
        private static ModelBindingContext CreateModelBindingContext(
            HttpActionContext actionContext,
            string modelName,
            Type type,
            IValueProvider vp
            )
        {
            Contract.Assert(actionContext != null);
            Contract.Assert(type != null);
            Contract.Assert(vp != null);

            ServicesContainer     cs = actionContext.ControllerContext.Configuration.Services;
            ModelMetadataProvider metadataProvider = cs.GetModelMetadataProvider();

            ModelBindingContext ctx = new ModelBindingContext()
            {
                ModelName             = modelName,
                FallbackToEmptyPrefix = false,
                ModelMetadata         = metadataProvider.GetMetadataForType(null, type),
                ModelState            = actionContext.ModelState,
                ValueProvider         = vp
            };

            return(ctx);
        }
コード例 #2
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            ValueProviderResult valueProviderResult = GetCompatibleValueProviderResult(
                bindingContext
                );

            if (valueProviderResult == null)
            {
                return(false); // conversion would have failed
            }

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
            object model = valueProviderResult.RawValue;

            ModelBindingHelper.ReplaceEmptyStringWithNull(bindingContext.ModelMetadata, ref model);
            bindingContext.Model = model;
            if (bindingContext.ModelMetadata.IsComplexType)
            {
                HttpControllerContext controllerContext = actionContext.ControllerContext;
                if (controllerContext == null)
                {
                    throw Error.Argument(
                              "actionContext",
                              SRResources.TypePropertyMustNotBeNull,
                              typeof(HttpActionContext).Name,
                              "ControllerContext"
                              );
                }

                HttpConfiguration configuration = controllerContext.Configuration;
                if (configuration == null)
                {
                    throw Error.Argument(
                              "actionContext",
                              SRResources.TypePropertyMustNotBeNull,
                              typeof(HttpControllerContext).Name,
                              "Configuration"
                              );
                }

                ServicesContainer services = configuration.Services;
                Contract.Assert(services != null);

                IBodyModelValidator   validator        = services.GetBodyModelValidator();
                ModelMetadataProvider metadataProvider = services.GetModelMetadataProvider();
                if (validator != null && metadataProvider != null)
                {
                    validator.Validate(
                        model,
                        bindingContext.ModelType,
                        metadataProvider,
                        actionContext,
                        bindingContext.ModelName
                        );
                }
            }

            return(true);
        }