解决方案:字符串的长度超过了POST操作在maxJsonLength属性上设置的值 [英] Solution: The length of the string exceeds the value set on the maxJsonLength property for POST action

查看:118
本文介绍了解决方案:字符串的长度超过了POST操作在maxJsonLength属性上设置的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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.

我尝试了以下方式

  1. 覆盖JsonResult
  2. 遵循了链接.在这里,我看不到脚本发送的数据.我在 base64 中得到了 NULL .
  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);
  }
})

错误

使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过了在maxJsonLength属性上设置的值.\ r \ n参数名称:input

推荐答案

创建一个 JsonDotNetValueProviderFactory.cs 并编写以下代码:

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);
 }
}

并在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆