问题
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
- Override JsonResult
- 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
回答1:
Create a JsonDotNetValueProviderFactory.cs
and 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 !!
来源:https://stackoverflow.com/questions/52701762/solution-the-length-of-the-string-exceeds-the-value-set-on-the-maxjsonlength-pr