创建需要类型信息MVC3 ValueProviderFactories? [英] Creating MVC3 ValueProviderFactories That Require Type Information?

查看:131
本文介绍了创建需要类型信息MVC3 ValueProviderFactories?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个protobuf的ValueProviderFactory为ASP MVC3。我已经成功地制定<一href="http://stackoverflow.com/questions/6885538/adding-custom-valueproviderfactories-to-asp-net-mvc3">how添加工厂的,但现在我已经无意中发现了一个更pressing问题。 这里就是当前的序列化发生在 JsonValueProviderFactory.cs

I'm attempting to write a Protobuf ValueProviderFactory for ASP MVC3. I've managed to work out how to add the factories, but now I've stumbled on a more pressing problem. Here is where the current serialization takes place in JsonValueProviderFactory.cs

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    object jsonData = serializer.DeserializeObject(bodyText);
    return jsonData;

因此​​,反序列化完成,没有任何类型的信息?什么样的对象呢 DeserializeObject 的回报?动态?它是如何知道的数据类型?我希望槽protobuf网在这里,但它显然需要一个类型做它的魔力!

So the deserialization is accomplished without any type information? What sort of object does DeserializeObject return? A Dynamic? How does it know the type of data? I was hoping to slot protobuf-net in here but it obviously needs a type to do its magic!

我还没有完成所有MVC3源看了,但我猜的映射类型发生在最后阶段,并没有办法知道在ValueProviderFactories类型?

I haven't looked through all the MVC3 source, but I'm guessing the mapping to types occurs at the final stage, and there is no way to know of the types in the ValueProviderFactories?

请问我不得不放弃,尽在行动转换?

Will I have to give up and do the conversion in the actions?

推荐答案

这里有几个问题。

有关如何 JavaScriptSerializer 的作品,你应该阅读<一个href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx"相对=nofollow>文档。该类试图推断基本类型(int,布尔,日期等),并返回词典&LT型,字符串,对象&gt; 对于更复杂的情况。此外,如果JSON的blob包含一个名为一个特殊的属性__type那么解串器将尝试创建该类型的对象。

For how JavaScriptSerializer works you should read the documentation. The class tries to infer the type for basic types (int, bool, date, etc) and returns Dictionary<string, object> for more complex cases. In addition if the JSON blob contains a special property called "__type" then the deserializer will try to create an object of that type.

现在是怎样工作的MVC。从请求到在控制器中使用的对象实例的映射值的过程被称为模型绑定。此被分成两个部分:ModelBinder的和ValueProviders。该模型粘合剂知道目标类型(如产品),试图从请求的值创建它的一个实例,然后填充它的属性。它通过询问ValueProviders这样做。例如,要在产品实例,它要求的价值提供者的名称的值的名称属性。该值提供查询得到的序列,并返回匹配(从查询字符串,POST数据,JSON请求体等)。

Now for how this works in MVC. The process of mapping values from a request to an object instance used in your controller is called model binding. This is split into two components: ModelBinder and ValueProviders. The model binder knows the target type (e.g. Product), tries to create an instance of it, and then populate its properties with values from the request. It does so by asking the ValueProviders. For example, to set the Name property on the Product instance it asks the value providers for the value of "Name". The value providers get queried in sequence and return a match (from query string, post data, JSON request body, etc).

有许多资源,这个在网络上,但在很短的价值提供者应该不是真的与类型有关。

There's lots of resources about this on the web, but in short value providers shouldn't really be concerned with types.

这篇关于创建需要类型信息MVC3 ValueProviderFactories?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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