1

I've following code in MVC. In my POST action params will have huge data. So, I changed web.config accordingly but, I'm getting ERROR. Actual problem is controller is not even hitting when POST is called.

I tried Following ways

  1. Override JsonResult
  2. followed Link. Here i couldn't see data which is sent from script. I'm getting NULL in base64.

controller.cs

 [System.Web.Mvc.HttpPost]
    public bool postImage(string base64)
    {
       return true;
    }

web.config

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

JavaScript

  $.ajax({
  type: "POST",
  url: 'http://localhost:21923/communities/postImage?base64=',
  contentType: "application/json; charset=utf-8",
  data: JSON.stringify(data),
  dataType: "json",
  success: function(data) {
    document.getElementById('test').click();
  },
  error: function(a, b, c) {
    console.log(a);
  }
})

Error

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.\r\nParameter name: input

Chanikya
  • 476
  • 1
  • 8
  • 22

1 Answers1

2

Create a JsonDotNetValueProviderFactory.csand write following code:

public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
  public override IValueProvider GetValueProvider(ControllerContext controllerContext)
 {
    if (controllerContext == null)
        throw new ArgumentNullException("controllerContext");

    if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
        return null;

    var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
    var bodyText = reader.ReadToEnd();

    return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
 }
}

and add following two lines in Global.asax.cs in Application_Start()

        ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
        ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

Source: Link

Thanks to Darin Dimitrov !!

If above code fails: Follow This might helps a lot

Thanks to dkinchen !!

Chanikya
  • 476
  • 1
  • 8
  • 22