什么是MVC的DefaultModelBinder在ASP.net网页API相当于? [英] What is the equivalent of MVC's DefaultModelBinder in ASP.net Web API?

查看:291
本文介绍了什么是MVC的DefaultModelBinder在ASP.net网页API相当于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立在ASP.Net的Web API自定义模型粘合剂。有很多关于如何从头开始做的资源,但我想利用现有的功能。

我在源左顾右盼上codePLEX,可以看到大量的modelbinders在那里,但大多数都是密封......即使如此,我不能工作了哪一个会在哪些情况下。

下面是我的API方法标题:

 公共异步任务< Htt的presponseMessage>邮报(长APPID,[FromBody]场场)

我想要做的主要是拦截它填充后,所有的基本属性,然后设置的基础上,我会在客户端进行设置HTTP请求头一些额外的值ModelBinder的。

正如我所说的,我很舒服创建定制modelbinders和modelbinderproviders和它们连接,位我在与试图重新使用所提供的现有功能有问题。

谢谢,皮特


解决方案

看看这里为扩展点<一个href=\"http://www.microsoft.com/en-us/download/details.aspx?id=36476&utm_source=feedburner&utm_medium=twitter&utm_campaign=Feed%3a%20MicrosoftDownloadCenter%20%28Microsoft%20Download%20Center%29#tm\">WebApi.

有对MVC的 DefaultModelBinder 中的WebAPI没有完全等效。

如果您使用的是 [FromBody] 属性则<一个href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/changeset/view/1b78397f32fc#src/System.Web.Http/ModelBinding/FormatterParameterBinding.cs\"><$c$c>FormatterParameterBinding将被调用和 MediaTypeFormatter 将用于构建模型。

有关URI路径和网址(查询参数)的模型粘合剂将调用<一个href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/changeset/view/1b78397f32fc#src/System.Web.Http/ModelBinding/ModelBinderParameterBinding.cs\"><$c$c>ModelBinderParameterBinding这将传递到任何一个 IValueProvider IModelBinder ...

所以...

在您的例子字段(根据内容类型协商的结果)将使用下列之一: XmlMediaTypeFormatter JsonMediaTypeFormatter FormUrlEn codedMediaTypeFormatter 。因此,取决于你使用各自设置例如自定义的这些序列化行为JSON.NET设置...,或实现自己或包MediaTypeFormatter。

在您的例子中,的appid 可能会被传递到 IValueProvider ...像<一个href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/changeset/view/1b78397f32fc#src/System.Web.Http/ValueProviders/Providers/ElementalValueProvider.cs\">ElementalValueProvider.

I want to create a custom model binder in ASP.Net Web API. There are plenty of resources on how to do this from scratch, but I want to leverage existing functionality.

I have looked around in the source on codeplex and can see plenty of modelbinders in there but most are sealed... and even then I can't work out which one would be used in which situations.

Here is my api method header:

public async Task<HttpResponseMessage> Post(long appId, [FromBody]Field field)

What I want to do is basically intercept the modelbinder after it has populated all the basic properties, and then set some extra values based on http request headers that I'll be setting on the client side.

As I say, I am comfortable with creating custom modelbinders and modelbinderproviders and wiring them in, the bit I'm having a problem with is trying to re-use the existing functionality provided.

Thanks, Pete

解决方案

Take a look here for the extension points for WebApi.

There is no exact equivalent for MVC's DefaultModelBinder in WebApi.

If you are using the [FromBody] attribute then the FormatterParameterBinding will be invoked and a MediaTypeFormatter will be used to construct your model.

The model binders for the URI path and Url (Query Params) will invoke ModelBinderParameterBinding which will pass on to either a IValueProvider or a IModelBinder...

So...

In your example Field (depending on the result of content type negotiation) will use one of these: XmlMediaTypeFormatter, JsonMediaTypeFormatter or FormUrlEncodedMediaTypeFormatter. It is therefore up to you to customise the serialisation behavior of these using their respective settings e.g. JSON.NET settings... or implement your own or wrapped MediaTypeFormatter.

In your example the appId will probably be passed on to a IValueProvider... like ElementalValueProvider.

这篇关于什么是MVC的DefaultModelBinder在ASP.net网页API相当于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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