예제 #1
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);
        }