![](/img/trans.png)
[英]The length of the string exceeds the value set on the maxJsonLength property
[英]Solution: The length of the string exceeds the value set on the maxJsonLength property for POST action
我在MVC中遵循代码。 在我的POST动作中, params
将包含大量数据。 因此,我相应地更改了web.config
,但出现了ERROR 。 实际问题是调用POST时控制器甚至没有命中。
I tried Following ways
base64
得到NULL 。 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);
}
})
错误
使用JSON JavaScriptSerializer进行序列化或反序列化时出错。 字符串的长度超过在maxJsonLength属性上设置的值。\\ r \\ n参数名称:input
创建一个JsonDotNetValueProviderFactory.cs
并编写以下代码:
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);
}
}
并在Global.asax.cs
的Application_Start()
添加以下两行
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
资料来源: 链接
感谢达林·迪米特洛夫 !
如果上面的代码失败:按照这个实力有很大帮助
感谢dkinchen !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.