I recently upgraded from a beta version of MVC4 to RC and ran into a small problem with the WebApi project. One of the first things I noticed was removing ServiceResolver. Before it was deleted, I used it to register the binding provider for a specific model as follows:
IEnumerable<object> modelBinderProviderServices = GlobalConfiguration.Configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider)); List<Object> services = new List<object>(modelBinderProviderServices) { new CustomDynamicObjectModelBinderProvider() }; GlobalConfiguration.Configuration.ServiceResolver.SetServices(typeof(ModelBinderProvider), services.ToArray());
The action that this provider of the binder of this model used has the following signature:
[HttpPut] public void Put(CustomDynamicObject model)
I tried replacing the old code with the following, but with no results:
GlobalConfiguration.Configuration.Services.Add(typeof(ModelBinderProvider), new CustomDynamicObjectModelBinderProvider());
When I tried to pass data to this action, the GetBinder method of the model provider is not called, and the model parameter is set to null. I was able to do the action using the required model module using the ModelBinder attribute, changing the signature of the Acion / method to the following
[HttpPut] public void Put([ModelBinder(typeof(CustomDynamicObjectModelBinderProvider))] CustomDynamicObject model)
While this works, I really would not want to use this syntax in all my controllers / actions.
I think I should mention that my middleware provider inherits from:
System.Web.Http.ModelBinding.ModelBinderProvider
I say this because I saw that there is another ModelBinderProvider class in the following namespace:
Microsoft.Web.Mvc.ModelBinding
Reminder: how to register custom binding module in MVC4 RC WebApi?