什么是MVC的DefaultModelBinder在ASP.net网页API相当于?

编程入门 行业动态 更新时间:2024-10-20 07:39:48
本文介绍了什么是MVC的DefaultModelBinder在ASP网页API相当于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

下面是我的API方法标题:

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

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

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

谢谢,皮特

解决方案

看看这里为扩展点WebApi.

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

如果您使用的是 [FromBody] 属性则FormatterParameterBinding将被调用和 MediaTypeFormatter 将用于构建模型。

有关URI路径和网址(查询参数)的模型粘合剂将调用ModelBinderParameterBinding这将传递到任何一个 IValueProvider 或 IModelBinder ...

所以...

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

在您的例子中,的appid 可能会被传递到 IValueProvider ...像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相当于?

本文发布于:2023-11-03 15:31:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555458.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网页   DefaultModelBinder   MVC   ASP   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!