Posting a large JSON object to ASP.NET MVC

13,091

Solution 1

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>

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.

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 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.

Environment: Windows 8, Visual Studio 2012 Update 3

Solution 2

I'm passing large JSON data which contains almost 1,50,00,000 characters to my controller, with below change in web.config:

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

This is working for me very nicely.....

Solution 3

You may need to bump the generic maximum request length (in kilobytes, example is 1GB)

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="1048576" />
  </system.web>
</configuration>

And if you're using IIS 7, probably bump max content length (in bytes, example is 1GB)

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
</system.webServer>

Solution 4

I had the same issue Posting large model object data (JSON) from client side to Server controller action. adding the below settings to config worked fine. thanks.

<appSettings>
    <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
Share:
13,091
nthpixel
Author by

nthpixel

ASP.NET, C#, MVC, Entity Framework, Angular, jQuery, anything web ...and corgis.

Updated on July 24, 2022

Comments

  • nthpixel
    nthpixel almost 2 years

    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);
        }
    });
    

    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?

    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>
    

    This didn't help.

    Is there something else I should be doing?

  • nthpixel
    nthpixel over 10 years
    I'm using IIS Express. Thanks, I tried both but it's still not getting to the server.