【问题标题】:MVC maxJsonLength errorMVC maxJsonLength 错误
【发布时间】:2018-05-18 22:29:51
【问题描述】:

我正在为 maxJsonLength 错误而苦苦挣扎。 我将非常长的 xml 字符串(~4MB)从控制器操作传递到视图中的 javascript 方法。此方法处理数据并使用 AJAX 将其发送回另一个控制器方法,这里出现错误。

我在控制器方法中的JsonResult 对象中设置了MaxJsonLength,并且将此字符串从控制器传递到视图没有问题。 但是当我在处理后尝试将其传回时,我得到了错误。

这是我准备数据的控制器方法:

private JsonResult PrepareXml(int someId)
{
     // preparing data...
     string xmlData = "..."; //(~4Mb)
     JsonResult res = Json(xmlData);
     res.MaxJsonLength = 10000000; // 10Mb
     return res;
}

这是我的控制器方法,它试图处理从 ajax 方法传递的数据:

private JsonResult GetProcesedXml(string resultXml)
{
     // This method is not ivoking
}

下面是我的脚本方法:

var data = {
        someId: rowId,
    };
$.ajax({
        type: "POST",
        url: "MyController/PrepareXml",
        data: data,
        contentType: "application/json; charset=utf-8",
        success: function (result) {
                // proces the result (big xml file)
                //...
                var processedResult = ""; // size of processedResult is a little bit bigger than 'result'
                var data2 = {
                    resultXml: processedResult
                };

                $.ajax({
                    type: "POST",
                    url: "MyController/GetProcesedXml",
                    data: data2,
                    contentType: "application/json; charset=utf-8",
                    success: function (r) {
                        alert('success');
                    },
                    error: function (data) {
                        alert(data.responseText);
                    }
                });
            }        
 });

我已经尝试过的:

<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" /> //(max value)
<requestLimits maxAllowedContentLength="2097151000" /> //(max value)
<httpRuntime maxRequestLength="10000" /> //(~10MB)

【问题讨论】:

  • @vikscool 谢谢,但不幸的是它没有帮助。但很高兴知道 maxRequestLength 以 KB 为单位。
  • 如果这没有帮助,那么您可以尝试分小部分发送请求,而不是单个大数据请求。

标签: javascript c# ajax asp.net-mvc


【解决方案1】:

我找到了一些解决这个问题的方法。 我已将 Ajax 方法中的内容类型更改为“text/plain”,并在我的 xml 文件数据上使用了 JSON.stringify。

var data2 = { resultXml: processedResult };

$.ajax({
    type: "POST",
    url: "MyController/GetProcesedXml",
    data: JSON.stringify(data2),
    contentType: "text/plain",
    success: function (r) {
                alert('success');
    },
});

另一个变化是在控制器中。我已经从输入流中读取文件并将其解析为 JSON 类型:

private JsonResult GetProcesedXml()
{
     JObject json = null;
     Stream request = Request.InputStream;
     using (StreamReader sr = new StreamReader(stream))
     {
           stream.Seek(0, System.IO.SeekOrigin.Begin);
           json = JObject.Parse(sr.ReadToEnd());
     }
     string xmlSigninigResult = json["resultXml"].ToString();
     // rest of the method
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2018-06-12
    相关资源
    最近更新 更多