【问题标题】:ASP.Net WebForm c# WebService - The length of the string exceeds the value set on the maxJsonLength propertyASP.Net WebForm c# WebService - 字符串长度超过maxJsonLength属性设置的值
【发布时间】:2018-07-05 15:03:26
【问题描述】:

我正在使用 ASP.Net WebForm 和 C# Web 服务,运行代码时出现以下错误;

Error during serialization or deserialization using the JSON JavaScriptSerializer. 
The length of the string exceeds the value set on the maxJsonLength property.

我已尝试将以下内容放入网络配置中,但未解决;

<system.web.extensions>
 <scripting>
  <webServices>
   <jsonSerialization maxJsonLength="2147483644"/>
  </webServices>
 </scripting>
</system.web.extensions>

我的网络服务;

        public class OpenRequisitions
    {
        public string string1 { get; set; }
        public string sstring2 { get; set; }
        public string string3 { get; set; }
        public string string4 { get; set; }
        public string string5 { get; set; }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<OpenRequisitions> GetOpenReqData(string ReqId, string RequisitionTitle, string City, string Country, string DateCreated)
    { 
        string connectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
        string commandTextGetOpenRequisitions = Properties.Queries.commandTextGetOpenRequisitions;
        List<OpenRequisitions> GetOpenRequisitionData = new List<OpenRequisitions>();
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(commandTextGetOpenRequisitions, con);
            command.CommandType = CommandType.Text;
            con.Open();
            SqlDataReader rdr = command.ExecuteReader();
            while (rdr.Read())
            {
                OpenRequisitions results = new OpenRequisitions();
                results.ReqId = rdr["string1"].ToString();
                results.RequisitionTitle = rdr["string2"].ToString();
                results.City = rdr["string3"].ToString();
                results.Country = rdr["string4"].ToString();
                results.DateCreated = rdr["string5"].ToString();

                GetOpenRequisitionData.Add(results);
            }
        }
        return GetOpenRequisitionData;
    }

【问题讨论】:

  • 我看到一篇旧文章说将maxJsonLength 设置为超过999999999 会使网站/服务崩溃。尝试减小尺寸。你知道实际退回了多少吗?

标签: c# asp.net json web-services webforms


【解决方案1】:

看起来问题与 JavaScriptSerializer 有关。您可以直接设置最大值。它不会从 web.config 继承值。

// Create an instance of your JavaScriptSerializer and set the MaxJsonLength.
var serializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 };

// Perform your serialization
serializer.Serialize("Your JSON Contents"); 

Source

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 2012-08-06
    • 2017-08-31
    • 2015-03-27
    • 2013-05-26
    • 2015-05-30
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多