繁体   English   中英

解决方案:字符串的长度超过了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 tried Following ways

  1. 覆盖JsonResult
  2. 关注链接 在这里,我看不到脚本发送的数据。 我在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.csApplication_Start()添加以下两行

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

资料来源: 链接

谢达林·迪米特洛夫

如果上面的代码失败:按照这个实力有很大帮助

感谢dkinchen

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM