c# - ASP.Net WebForm c# WebService - 字符串长度超过maxJsonLength属性设置的值
问题描述
我正在使用 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;
}
解决方案
看起来问题与 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");
推荐阅读
- npm - 是否可以将项目的 node_modules 存档,然后将其上传到 Nexus Repository Manager?
- javascript - 如何解决 Codility equiLeader 挑战
- type-conversion - 在 Julia 中绘制 ForwardDiff 的输出
- xml - XSLT 1.0 - 基于来自不同节点的属性值创建节点
- javascript - 如何在json文件javascript中写入数据而不是在节点js中
- javascript - 带有 Electron 的 TestCafe 抛出“未捕获的类型错误:无法读取未定义的属性‘源’”
- xml - Jaxb:验证子元素
- javascript - 如何在承诺中添加超时?
- javascript - 通过 GetElementByName 获取文本区域的值
- macos - Flutter 找不到 tools.jar