使用 JSON.NET 作为 ASP.NET MVC 3 中的默认 JSON 序列化器 - 可能吗?
是否可以使用 JSON.NET 作为 ASP.NET MVC 3 中的默认 JSON 序列化程序?
根据我的研究,似乎实现这一点的唯一方法是 将 ActionResult 扩展为 MVC3中的JsonResult不是虚拟的...
我希望用ASP .NET MVC 3 将有一种方法来指定可插入提供程序以序列化为 JSON。
想法?
Is it possible to use JSON.NET as default JSON serializer in ASP.NET MVC 3?
According to my research, it seems that the only way to accomplish this is to extend ActionResult as JsonResult in MVC3 is not virtual...
I hoped that with ASP.NET MVC 3 that there would be a way to specify a pluggable provider for serializing to JSON.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我相信最好的方法是 - 正如链接中所述 - 直接扩展 ActionResult 或扩展 JsonResult 。
至于控制器上不是虚拟的方法 JsonResult 是不正确的,只需选择正确的重载即可。这效果很好:
编辑 1:一个 JsonResult 扩展...
编辑 2:我按照下面的建议删除了对数据为空的检查。这应该会让新版本的 JQuery 满意,并且看起来是明智之举,因为响应可以无条件反序列化。但请注意,这不是 ASP.NET MVC 的 JSON 响应的默认行为,当没有数据时,它会使用空字符串进行响应。
I believe the best way to do it, is - as described in your links - to extend ActionResult or extend JsonResult directly.
As for the method JsonResult that is not virtual on the controller that's not true, just choose the right overload. This works well:
EDIT 1: A JsonResult extension...
EDIT 2: I removed the check for Data being null as per the suggestions below. That should make newer versions of JQuery happy and seems like the sane thing to do, as the response can then be unconditionally deserialized. Be aware though, that this is not the default behavior for JSON responses from ASP.NET MVC, which rather responds with an empty string, when there's no data.
我实现了这个,不需要基本控制器或注入。
我使用操作过滤器将 JsonResult 替换为 JsonNetResult。
在 Global.asax.cs Application_Start() 中,您需要添加:
为了完成起见,这是我从其他地方获取的 JsonNetResult 扩展类,我对其进行了轻微修改以获得正确的流支持:
I implemented this without the need of a base controller or injection.
I used action filters to replace the JsonResult with a JsonNetResult.
In the Global.asax.cs Application_Start() you would need to add:
For completion's sake, here is my JsonNetResult extention class that I picked up from somewhere else and that I modified slightly to get correct steaming support:
使用Newtonsoft的JSON转换器:
Use Newtonsoft's JSON converter:
我知道这是在问题得到解答之后很久,但我使用了不同的方法,因为我使用依赖项注入来实例化我的控制器。
我已将 IActionInvoker (通过注入控制器的 ControllerActionInvoker 属性)替换为覆盖 InvokeActionMethod 方法的版本。
这意味着控制器继承不会发生变化,当我升级到 MVC4 时,可以通过更改所有控制器的 DI 容器注册来轻松删除它
--- 编辑 - 更新以显示控制器的容器注册。我这里用的是Unity。
I know this is well after the question has been answered, but I'm using a different approach as I am using dependency injection to instantiate my controllers.
I have replaced the IActionInvoker ( by injecting the controller's ControllerActionInvoker Property ) with a version that overrides the InvokeActionMethod method.
This means no change to controller inheritance and it can be easily removed when I upgrade to MVC4 by altering the DI container's registration for ALL controllers
--- EDIT - Updated to show container registration for controllers. I'm using Unity here.
扩展 https://stackoverflow.com/users/183056/sami-beyoglu 的答案,如果您设置Content 类型,那么 jQuery 将能够为您将返回的数据转换为对象。
Expanding on the answer from https://stackoverflow.com/users/183056/sami-beyoglu, if you set the Content type, then jQuery will be able to convert the returned data into an object for you.
我的帖子可能会对某人有所帮助。
My Post may help someone.
我制作了一个使 Web 服务操作类型安全且简单的版本。你可以像这样使用它:
类:
I made a version that makes web service actions type-safe and simple. You use it like this:
The class: