gpt4 book ai didi

asp.net-mvc-3 - 如何在 ASP.NET MVC 3+ 应用程序中使用 Json.NET 作为默认反序列化器?

转载 作者:行者123 更新时间:2023-12-04 02:26:38 25 4
gpt4 key购买 nike

关于如何将 Json.NET 库替换为 default serializer 的资源有很多。在 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());

}

当反序列化 Controller 操作中的传入参数时,例如从 jQuery AJAX 调用中,我如何告诉我的应用程序使用 Json.NET?
$.ajax({
type : 'POST',
data : { foo : 'bar' }
});

我试过适应 MediaTypeFormatters通过调整 this resource from Rick Strahl 进入我的代码,但这也不起作用。请注意,我不在 WebAPI 环境中 --- 但我希望有一种解决方案适用于普通 Controller应该在 ApiController 中工作(尽管调整最少) .

最佳答案

您需要实现自定义 ValueProviderFactory 并删除标准 JsonValueProviderFactory。

这里有一些示例代码:http://json.codeplex.com/discussions/347099但是阅读评论后,我不能 100% 确定它会正确处理字典。

关于asp.net-mvc-3 - 如何在 ASP.NET MVC 3+ 应用程序中使用 Json.NET 作为默认反序列化器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12374991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com