MVC 4中Json Post的大小限制增加

时间:2015-02-02 07:06:22

标签: json asp.net-mvc-4 serialization json.net

我见过&amp;尝试了很多代码来增加JSON限制,如同 1. web.config <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="2147483647"/> </webServices> </scripting> </system.web.extensions>

2.JSonControllerFactory

    public sealed class CustomJsonValueProviderFactory : ValueProviderFactory
{

    private static void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, object value)
    {
        IDictionary<string, object> d = value as IDictionary<string, object>;
        if (d != null)
        {
            foreach (KeyValuePair<string, object> entry in d)
            {
                AddToBackingStore(backingStore, MakePropertyKey(prefix, entry.Key), entry.Value);
            }
            return;
        }

        IList l = value as IList;
        if (l != null)
        {
            for (int i = 0; i < l.Count; i++)
            {
                AddToBackingStore(backingStore, MakeArrayKey(prefix, i), l[i]);
            }
            return;
        }

        // primitive
        backingStore[prefix] = value;
    }
  

在GetDeserializedObject()中,我将 bodytext 设为空,无法设置max属性。

    private static object GetDeserializedObject(ControllerContext controllerContext)
    {
        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
        {
            // not JSON request
            return null;
        }

        StreamReader reader = new    StreamReader(controllerContext.HttpContext.Request.InputStream);
        string bodyText = reader.ReadToEnd();
        if (String.IsNullOrEmpty(bodyText))
        {
            // no JSON data
            return null;
        }

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.MaxJsonLength = int.MaxValue; //increase MaxJsonLength.  This could be read in from the web.config if you prefer
        object jsonData = serializer.DeserializeObject(bodyText);
        return jsonData;
    }

}

脚本[向下数据:jQuery(&#34;#geomaster&#34;)。serialize()包含数据。如果低于100点,则数据保存成功。如果它超过100分,没有击中控制器SaveGeodata方法(我的意思是不发布到控制器)]。

jQuery.ajax({
            type: "GET",
            url: "SaveGeodata",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: jQuery("#geomaster").serialize(),
            success: function (data) {
                alert("Geofence Created Successfully");

            },
            error: function (msg) {
                alert("Error");
            }

        });

有什么办法可以在脚本本身附加maxJsonSize属性。 任何其他可能有助于将最大数据发布到控制器的方法都非常感谢。

0 个答案:

没有答案