简体   繁体   中英

How to register a custom modelbinder in MVC4 RC WebApi

I recently updated from MVC4 beta to the RC and have encountered a small issue with a WebApi project. One of the first things I noticed is that the ServiceResolver was removed. Before it was removed I was using it to register a custom model binder provider in the following way:

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 which took advantage of this model binder provider has the following signature:

[HttpPut]
public void Put(CustomDynamicObject model)

I tried to replace the old code with the following but with no results:

GlobalConfiguration.Configuration.Services.Add(typeof(ModelBinderProvider), new CustomDynamicObjectModelBinderProvider());

When I tried to PUT data to the given action, the model provider's GetBinder method is not called and the model paramter is set to null. I was able to make the action use the wanted modelbinder using the ModelBinder attribute by changing the signature of the Acion/method to the following

[HttpPut]
public void Put([ModelBinder(typeof(CustomDynamicObjectModelBinderProvider))] CustomDynamicObject model)

While this works I really wouldn't like to have to use this syntax in all my controllers/actions.

I think I should mention that my model binder provider is inheriting from:

System.Web.Http.ModelBinding.ModelBinderProvider

I am saying this because I saw that there is another ModelBinderProvider class in the following namespace:

Microsoft.Web.Mvc.ModelBinding

To recap: How to register a custom modelbinder in MVC4 RC WebApi ?

The model binding rules in Web API have changed since the Beta. The new rules are described in Mike Stall's post . Model binding for complex types now only works if you explicitly add a ModelBinder attribute to the parameter.

The parameter binding mechanism has changed again since the Release Candidate, so you probably want to wait for RTM before making too many changes. There are a couple of other options that might work in the RC version - depending on what the source is of the data you are trying to bind (query string or request body).

  • If the source of your data is the request body then you can create a custom MediaTypeFormatter rather than a model binder:

  • If your data comes from the querystring and you want to avoid explicitly including a [ModelBinder] attribute on your parameters, then you may be able to use a combination of a custom TypeConverter and a custom IValueProvider .

In the RTM version (or current nightlies), you will be able to use a custom HttpParameterBinding if the other options don't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM