Ric*_*gan 11 json.net asp.net-mvc-3
关于如何将Json.NET库替换为ASP.NET MVC应用程序中的默认序列化程序有很多资源,但是,就我而言,我找不到一个如何将其设置为默认反序列化器.
为了说明这一点,这里是一些模板代码:
// how to use Json.NET when deserializing
// incoming arguments?
V
public ActionResult SomeAction ( Foo foo ) {
// this piece of code has lots of resources
// on how to override the default Javascript serializer
return Json(new Bar());
}
Run Code Online (Sandbox Code Playgroud)
如何在控制器操作中反序列化传入参数(例如,从jQuery AJAX调用)时,如何告诉我的应用程序使用Json.NET?
$.ajax({
type : 'POST',
data : { foo : 'bar' }
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试MediaTypeFormatters通过从Rick Strahl调整此资源来适应我的代码,但这也不起作用.请注意,我不在WebAPI环境中 - 但我希望一个适用于正常的解决方案Controller应该可以工作(虽然只需要很少的调整)ApiController.
您需要实现自定义 ValueProviderFactory 并删除标准 JsonValueProviderFactory。
这里有一些示例代码:http://json.codeplex.com/discussions/347099但是,通过阅读注释,我不能 100% 确定它会正确处理字典。