解决方案:字符串的长度超过了POST操作在maxJsonLength属性上设置的值 [英] 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'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.
我尝试了以下方式
- 覆盖JsonResult
- 遵循了链接.在这里,我看不到脚本发送的数据.我在
base64
中得到了 NULL .
- 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);
}
})
错误
使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过了在maxJsonLength属性上设置的值.\ r \ n参数名称:input
推荐答案
创建一个 JsonDotNetValueProviderFactory.cs
并编写以下代码:
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);
}
}
并在 Application_Start()
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
来源:链接
感谢 Darin Dimitrov !
如果上述代码失败:请遵循这可能会很有帮助
If above code fails: Follow This might helps a lot
感谢 dkinchen !
这篇关于解决方案:字符串的长度超过了POST操作在maxJsonLength属性上设置的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!