示例#1
0
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     if (WebApiEnabledAttribute.IsDefined(controllerContext.Controller))
     {
         if (!controllerContext.RouteData.Values.ContainsKey(bindingContext.ModelName) && controllerContext.HttpContext.Request.HasBody())
         {
             ContentType requestFormat = controllerContext.RequestContext.GetRequestFormat();
             object      model;
             if (TryBindModel(controllerContext, bindingContext, requestFormat, out model))
             {
                 bindingContext.ModelMetadata.Model = model;
                 MyDefaultModelBinder dmb = new MyDefaultModelBinder();
                 dmb.CallOnModelUpdated(controllerContext, bindingContext);
                 if (!MyDefaultModelBinder.IsModelValid(bindingContext))
                 {
                     List <ModelError> details = new List <ModelError>();
                     foreach (ModelState ms in bindingContext.ModelState.Values)
                     {
                         foreach (ModelError me in ms.Errors)
                         {
                             details.Add(me);
                         }
                     }
                     HttpException failure = new HttpException((int)HttpStatusCode.ExpectationFailed, "Invalid Model");
                     failure.Data["details"] = details;
                     throw failure;
                 }
                 return(model);
             }
             throw new HttpException((int)HttpStatusCode.UnsupportedMediaType, String.Format(CultureInfo.CurrentCulture, MvcResources.Resources_UnsupportedMediaType, (requestFormat == null ? String.Empty : requestFormat.MediaType)));
         }
     }
     return(this._inner.BindModel(controllerContext, bindingContext));
 }
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     if (WebApiEnabledAttribute.IsDefined(controllerContext.Controller))
     {
         if (!controllerContext.RouteData.Values.ContainsKey(bindingContext.ModelName) && controllerContext.HttpContext.Request.HasBody())
         {
             ContentType requestFormat = controllerContext.RequestContext.GetRequestFormat();
             object model;
             if (TryBindModel(controllerContext, bindingContext, requestFormat, out model))
             {
                 bindingContext.ModelMetadata.Model = model;
                 MyDefaultModelBinder dmb = new MyDefaultModelBinder();
                 dmb.CallOnModelUpdated(controllerContext, bindingContext);
                 if (!MyDefaultModelBinder.IsModelValid(bindingContext))
                 {
                     List<ModelError> details = new List<ModelError>();
                     foreach (ModelState ms in bindingContext.ModelState.Values)
                     {
                         foreach (ModelError me in ms.Errors)
                         {
                             details.Add(me);
                         }
                     }
                     HttpException failure = new HttpException((int)HttpStatusCode.ExpectationFailed, "Invalid Model");
                     failure.Data["details"] = details;
                     throw failure;
                 }
                 return model;
             }
             throw new HttpException((int)HttpStatusCode.UnsupportedMediaType, String.Format(CultureInfo.CurrentCulture, MvcResources.Resources_UnsupportedMediaType, (requestFormat == null ? String.Empty : requestFormat.MediaType)));
         }
     }
     return this._inner.BindModel(controllerContext, bindingContext);
 }
示例#3
0
 public static object InvokeAction(this Controller controller, string actionName)
 {
     IModelBinder modelBinder = new MyDefaultModelBinder();
     ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(controller.GetType());
     ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controller.ControllerContext, actionName);
     Dictionary<string, object> arguments = new Dictionary<string, object>();
     foreach (ParameterDescriptor parameterDescriptor in actionDescriptor.GetParameters())
     {
         string modelName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
         ModelBindingContext bindingContext = new ModelBindingContext
         {
             FallbackToEmptyPrefix = parameterDescriptor.BindingInfo.Prefix == null,
             ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterDescriptor.ParameterType),
             ModelName = modelName,
             ModelState = controller.ModelState,
             ValueProvider = controller.ValueProvider
         };
         object argument = modelBinder.BindModel(controller.ControllerContext, bindingContext);
         arguments.Add(parameterDescriptor.ParameterName, argument);
     }
     return actionDescriptor.Execute(controller.ControllerContext, arguments);
 }
        public static object InvokeAction(this Controller controller, string actionName)
        {
            IModelBinder                modelBinder          = new MyDefaultModelBinder();
            ControllerDescriptor        controllerDescriptor = new ReflectedControllerDescriptor(controller.GetType());
            ActionDescriptor            actionDescriptor     = controllerDescriptor.FindAction(controller.ControllerContext, actionName);
            Dictionary <string, object> arguments            = new Dictionary <string, object>();

            foreach (ParameterDescriptor parameterDescriptor in actionDescriptor.GetParameters())
            {
                string modelName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
                ModelBindingContext bindingContext = new ModelBindingContext
                {
                    FallbackToEmptyPrefix = parameterDescriptor.BindingInfo.Prefix == null,
                    ModelMetadata         = ModelMetadataProviders.Current.GetMetadataForType(null, parameterDescriptor.ParameterType),
                    ModelName             = modelName,
                    ModelState            = controller.ModelState,
                    ValueProvider         = controller.ValueProvider
                };
                object argument = modelBinder.BindModel(controller.ControllerContext, bindingContext);
                arguments.Add(parameterDescriptor.ParameterName, argument);
            }
            return(actionDescriptor.Execute(controller.ControllerContext, arguments));
        }