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 IController CreateController(RequestContext requestContext, string controllerName) { IController ic = _inner.CreateController(requestContext, controllerName); Controller c = ic as Controller; if (c != null && WebApiEnabledAttribute.IsDefined(c)) { IActionInvoker iai = c.ActionInvoker; ControllerActionInvoker cai = iai as ControllerActionInvoker; if (cai != null) { c.ActionInvoker = new ResourceControllerActionInvoker(); string actionName = requestContext.RouteData.Values["action"] as string; if (String.IsNullOrEmpty(actionName)) { // set it to a well known dummy value to avoid not having an action as that would prevent the fixup // code in ResourceControllerActionInvoker, which is based on ActionDescriptor, from running requestContext.RouteData.Values["action"] = RestActionToken; } } } return(ic); }