0

I got the following error message on submitting HTML form containing a lot of data. I have added web.config settings as suggested in this answer, but the error remains.

"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

What am I possibly missing? How to check current maxJsonLength setting during debug? (to validate whether or not the web.config setting is applied or ignored).


View (the HTML form) :

using (Html.BeginForm("Submit", "Form", FormMethod.Post, new { @id = "submitForm", @enctype = "multipart/form-data" }))
{
    ....
}

Javascript (on button submit clicked) :

document.getElementById('submitForm').submit();

Controller :

[ValidateInput(false)]
public ActionResult Submit(EprocModel eprocModel)
{
    ....
}

Global.asax (where the error appeared) :

On submitting the form in debug mode, breakpoint at the beginning of controller method Submit() doesn't hit. Instead, execution enters Application_Error() in global.asax with error.Message value containing the error message posted above. And at this point, I checked that RequestContext's Content Length is 31325596, still below maxJsonLength set in web.config :

private void Application_Error(object sender, EventArgs ea)
{
    var error = Server.GetLastError();
    ....
}
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • just to confirm, you're using MVC 2? as in version 2.x.x DLLs, right? – Raja Nadar Feb 23 '16 at 04:21
  • yes, as I checked in web.config : ` – har07 Feb 23 '16 at 04:24
  • You even linked the answer to the question, the first answer says [*if you are returning JSON from a Controller method, make sure you read this SO answer below*](http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config/7207539#7207539). – Erik Philips Feb 23 '16 at 05:34
  • @ErikPhilips The controller method (`Submit()`) in my case doesn't return JSON, that's why I'm confused and decided to post this question. This is a legacy apps though, maybe another controller method which return JSON has been called before (I can see 2 JSON returning methods in the same controller), I'm checking right now. Thanks – har07 Feb 23 '16 at 05:41
  • @ErikPhilips I have checked, the two controller methods which return JSON are not called on submit button clicked. So, there is nowhere to implement the solution you linked to in the above comment, just like I thought when I read it for the 1st time (before posting this question) – har07 Feb 23 '16 at 06:34
  • @ErikPhilips Any other idea? – har07 Feb 23 '16 at 06:41
  • 1
    @har07 thanks for the confirmation. can you try a source code copy of the JsonValueProviderFactory class and set its maxJson length to int32.maxvalue, and then register it in your MVC pipeline. ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); i think that is the class that gets invoked when your controller gets incoming JSON. (i know that outgoing json doesn't help you) – Raja Nadar Feb 23 '16 at 07:44
  • @RajaNadar Thanks for your suggestion, I tried with source code from [this link](https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/JsonValueProviderFactory.cs), registered the `MyJsonValueProviderFactory` in `Application_Start()` in global.asax, but the value provider didn't get called. Additional info, I couldn't see MVC's `JsonValueProvider` listed in `ValueProviderFactories.Factories` at the time the error occured, but I saw `FormValueProvider` there.. – har07 Feb 23 '16 at 08:49
  • @RajaNadar After I removed `FormValueProvider` from `ValueProviderFactories.Factories`, the error disappear and I got `eprocModel` properties are all `null` (or other default value), so I feel more confident that this has something to do with `FormValueProvider`. I'm clueless though on how `FormValueProvider` can possibly throw JSON JavaScriptSerializer related error and so on where to alter `maxJsonLength` setting in this case – har07 Feb 23 '16 at 12:13

1 Answers1

1

set the json result to be larger

     var jsonResult = Json(data, JsonRequestBehavior.AllowGet);
     jsonResult.MaxJsonLength = int.MaxValue;
     return jsonResult;

https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult.maxjsonlength%28v=vs.118%29.aspx

M pollack
  • 64
  • 4
  • Thanks for reply, but this isn't applicable to my case, because my controller method doesn't return JSON.. – har07 Feb 23 '16 at 06:48