如何注册在MVC4 RC的WebAPI的自定义模型绑定器 [英] How to register a custom modelbinder in MVC4 RC WebApi

查看:118
本文介绍了如何注册在MVC4 RC的WebAPI的自定义模型绑定器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我从公测MVC4到RC更新,遇到了一个小问题用的WebAPI项目。其中的第一件事我注意到的是,ServiceResolver被删除。之前它被删除,我用它来注册通过以下方式自定义模型绑定提供商:

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)

我试图来取代旧的code以下,但没有结果:

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

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

当我试图将数据放置到定操作,该模型提供的GetBinder方法不叫,模型放慢参数设置为null。我能够通过改变Acion /方法的签名,以使动作用使用ModelBinder的属性通缉ModelBinder的以下

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

我说,因为我看到有在以下命名空间中的另一个ModelBinderProvider类:

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

Microsoft.Web.Mvc.ModelBinding

要回顾一下:如何MVC4 RC的WebAPI注册自定义ModelBinder的

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

推荐答案

该模型结合在网页API规则公测以来的变化。新规则在<一个描述href=\"http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx\">Mike失速的帖子。模型的复杂类型绑定,如果你明确地添加 ModelBinder的属性参数现在才起作用。

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.

参数绑定机制已经改变的再次的自发行候选版本,所以你可能要做出太多改变之前等待RTM。有一对夫妇的其他选项可能在RC版本的工作 - 根据源是您试图绑定(查询字符串或请求体)中的数据是什么

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).


  • 如果您的数据源请求主体,那么你可以创建一个自定义的 MediaTypeFormatter ,而不是一个模型绑定:

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

如果您的数据来自查询字符串,并要避免明确的,包括你的参数 [ModelBinder的] 属性,那么您可以使用的组合<一href=\"http://www.hanselman.com/blog/TypeConvertersTheresNotEnoughTypeDescripterGetConverterInTheWorld.aspx\">custom 的TypeConverter 和<一个href=\"http://blogs.msdn.com/b/jmstall/archive/2012/04/23/how-to-create-a-custom-value-provider-in-webapi.aspx\">custom IValueProvider

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.

在RTM版本(或电流Nightlies版),你就可以使用自定义的<一个href=\"http://blogs.msdn.com/b/jmstall/archive/2012/05/11/webapi-parameter-binding-under-the-hood.aspx\"><$c$c>HttpParameterBinding如果其他选项不起作用。

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

这篇关于如何注册在MVC4 RC的WebAPI的自定义模型绑定器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆