将自定义 ValueProviderFactories 添加到 ASP.NET MVC3?
我试图尝试将 Protobuf ValueProviderFactory 添加到 MVC3,以便我可以选择 MIME 类型并将原始数据反序列化为操作参数的对象。我还可以使用它来更改默认的 Json 序列化器。
查看 JsonValueProviderFactory.cs,这应该不会太困难,但工厂似乎都是硬编码的。
对于 Protobuf,我也许可以使用 IValueProvider 做一些事情,但我什至还没有检查 MVC3 在收到 MIME 类型的 application/x-protobuf
时会做什么。
我以正确的方式处理这件事吗?
更新
我发现这篇博客文章其中讨论了创建 IValueProvider
。然后它在底部提到这在 MCV2 周围发生了变化。他将其更改为 ValueProviderFactory
并调用:
ValueProviderFactories.Factories.Add(new HttpCookieValueProviderFactory());
但在 MVC3 中此属性是只读的。
I was looking to try and add a Protobuf ValueProviderFactory to MVC3 so that I could pick out the MIME type and deserialize the raw data into objects for action parameters. I could also use this to change the default Json serializer.
Looking at JsonValueProviderFactory.cs
this shouldn't be too difficult, but the factories all appear to be hard-coded.
For Protobuf I may be able to do something with an IValueProvider but I haven't even checked yet what MVC3 does when it recieves an MIME type of application/x-protobuf
.
Am I going about this the right way?
UPDATE
I found this blog post that talks about creating an IValueProvider
. It then mentions at the bottom that this changed around MCV2. He changed it to a ValueProviderFactory
and calls :
ValueProviderFactories.Factories.Add(new HttpCookieValueProviderFactory());
But in MVC3 this property is read only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明它不是只读的,您可以按如下方式添加提供程序:
如果我自己检查的话我就会知道这一点!
我今天做了更多搜索,这篇博客文章似乎表明
DependencyResolver
将找到继承ValueProviderFactory
的任何类。我使用 MEF 进行依赖项解析,因此我只需添加一个 Export 属性,它就会被自动选取。我现在有一个进一步的问题为protobuf-net编写自定义ValueProviderFactory 。
It turns out that it is not read only and you can add providers as follows:
I would have know this had I checked myself!
I've done some more searching today, and this blog post seems to suggest that the
DependencyResolver
will find any classes that inheritValueProviderFactory
. I'm using MEF for dependency resolution so I can just add an Export attribute and it'll get picked up automatically.I now have a further issue writing a custom ValueProviderFactory for protobuf-net.