将大型 JSON 对象发布到 ASP.NET MVC [英] Posting a large JSON object to ASP.NET MVC

查看:19
本文介绍了将大型 JSON 对象发布到 ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 KnockoutJS 的映射插件将我的模型转换为 Knockout 对象.但是我在将大型 json 对象发送回服务器时遇到了问题.我的 ajax 调用如下所示:

I'm using KnockoutJS's mapping pluggin to convert my model into Knockout objects. But I'm having issues sending the large json object back to the server. My ajax call looks like this:

$.ajax({
    url: "/home/DoStuff",
    type: "POST",
    data: JSON.stringify({ deal: ko.toJS(myObjectViewModel) }),
    contentType: "application/json",
    dataType: "json",
    success: function (result) {
        console.log(result);
    },
    error: function (xhr, status, message) {
        console.log(xhr);
    }
});

执行此脚本永远不会命中控制器中的 DoStuff 操作.当我用 Firebug 检查时,POST 一直在旋转.在 Firebug 的 Net 选项卡中,它说 Post Body 为 159.9 KB,Total Sent 为 165.1 KB(包括标题).如果发送了,为什么没有打到我代码中的断点?

Executing this script never hits the DoStuff action in the controller. When I inspect with Firebug, the POST just keeps spinning. In the Net tab of Firebug, it says the Post Body is 159.9 KB and Total Sent is 165.1 KB (including the headers). If it was sent, why is it not hitting my breakpoint in the code?

但是当我只发送 myObjectViewModel 的一个属性时,它发布得很好,一切都成功了.所以这让我假设问题在于发布的数据的大小.所以我尝试增加 maxJsonLength.

But when I instead send just a property of myObjectViewModel, it posts fine and everything is successful. So that leads me to assume the issue is with the size of the data being posted. So I tried increasing the maxJsonLength.

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483644"></jsonSerialization>
        </webServices>
    </scripting>
</system.web.extensions>

这没有帮助.

还有什么我应该做的吗?

Is there something else I should be doing?

推荐答案

我猜我的对象超出了 ASP.NET 允许反序列化的最大成员数.我添加了以下内容以增加它并且它有效.

I guess my object exceeded the maximum number of members ASP.NET was allowed to deserialize. I added the below to increase it and it works.

<appSettings>
    <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

更新:所以还有更多内容,因为在我让它工作后,最终它似乎无缘无故地停止工作.Firebug 会再次显示刚刚旋转的帖子,而不会碰到后端的代码.只有在我关闭 Firefox、终止 IIS Express 进程并关闭 Visual Studio 之后,帖子才重新开始工作.但关闭 VS 似乎是关键.在我让它再次工作后,更改代码并重新运行应用程序会使问题再次出现.出现问题时,即使硬刷新也不会加载页面.它只会坐在那里旋转.只有在我终止 IIS Express 进程并从 VS 重新运行后,页面才会重新加载.

UPDATE: So there was more to it because after I got it working, eventually it would stop working for what seems to be no reason. Firebug would again show the post just spinning without it hitting the code on the back end. Only after I closed Firefox, killed the IIS Express process and closed Visual Studio, did the post start working again. But closing VS seems to be key. After I got it working again, changing the code and re-running the app make the issue re-appear. When the issue occurs, not even a hard refresh will load the page. It'll just sit there spinning. The page will only reload after I kill the IIS Express process and rerun from VS.

所以我放弃了 IIS Express 并在我的机器上使用了完整的 IIS.看起来这解决了问题.每次发布大型json数据都没有失败......即使在更改代码并重新编译之后也是如此.

So I dumped IIS Express and used full IIS on my machine instead. Looks like this solves the problem. Posting the large json data goes through every time without fail...even after changing code and recompiling.

IIS Express 似乎可以很好地处理少量 json 的发布,但在较大的集合上会阻塞.我还没有进行足够彻底的测试来 100% 确认这一点,但我知道使用完整的 IIS 性能更可靠.不过还没有测试过 VS 的内部网络服务器.

IIS Express seems to handle posting small amounts of json fine but chokes on larger sets. I haven't tested thoroughly enough to 100% confirm this but I do know performance is more reliable with full IIS. Haven't tested VS's internal web server though.

环境:Windows 8,Visual Studio 2012 更新 3

Environment: Windows 8, Visual Studio 2012 Update 3

这篇关于将大型 JSON 对象发布到 ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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