将大型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的网络"选项卡中,它说邮件正文"为159.9 KB,已发送总数"为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的内部Web服务器.

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 Update 3

Environment: Windows 8, Visual Studio 2012 Update 3