【问题标题】:An exception of type 'System.InvalidOperationException' occurred in System.Web.Extensions.dll but was not handled in user codeSystem.Web.Extensions.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理
【发布时间】:2014-04-22 19:56:02
【问题描述】:

为什么这部分代码会出现这个错误?

这部分

           var data = serializer.Deserialize<EmailTemplate>(httpRequest.Form["data"].ToString());

我的程序

public int UpdateEmailTemplate()
    {

        HttpResponseMessage result = null;
        ObjectService uploadFile = new ObjectService();
        List<ActiveUp.Net.Mail.Attachment> attachment = new List<ActiveUp.Net.Mail.Attachment>();

        var httpRequest = HttpContext.Current.Request;

        if (httpRequest.Form["data"] != null)
        {
            var serializer = new JavaScriptSerializer();
            var data = serializer.Deserialize<EmailTemplate>(httpRequest.Form["data"].ToString());

            if (httpRequest.Files.Count > 0)
            {
                var docfiles = new List<string>();
                foreach (string file in httpRequest.Files)
                {
                    MemoryStream target = new MemoryStream();
                    httpRequest.Files[file].InputStream.CopyTo(target);

                    uploadFile.AddObject(data.Id, "SU_ENTITY_MSG_TEMPLATE","", target.GetBuffer(), httpRequest.Files[file].FileName);
                }
            }

            AdminService List = new AdminService();
            return List.UpdateEmailTemplate(data);
    }

我的班级

public class EmailTemplate
{
   public int Id;
   public string TypeObject;
   public int? idObject;
   public string ObjectName;
   public string IdTeam;
   public string IdTask;
   public string Team;
   public string task;
   public string Title;
   public string Subject;
   public string dataHtml;
   public List<Objects> fileListRequest;
}

【问题讨论】:

  • 请显示异常的详细信息和EmailTemplate的定义。
  • 我确实有这个错误消息 {"Type 'System.String' is not supported for deserialization of an array."} 我尝试使用 {get;set} 但它不起作用
  • 您尝试反序列化的字符串很可能与EmailTemplate 的定义不匹配。您是否尝试从 JSON 反序列化?请显示无法反序列化的字符串;请尝试查找导致讨论错误的最小长度的字符串。
  • 我确实有这个字符串 "{\"Id\":50021,\"TypeObject\":\"Team\",\"idObject\":null,\"ObjectName\":\ "Dep/arr 任务\",\"IdTeam\":\"4\",\"IdTask\":null,\"Team\":null,\"task\":null,\"Title\": \"SWA\",\"主题\":\"SWA\",\"dataHtml\":\"

    x\\n\",\"fileListRequest\":[] ,\"

  • 如果您从EmailTemplate 的定义中删除fileListRequest,您会得到同样的错误吗?

标签: c#


【解决方案1】:

说的很清楚,内容是:

httpRequest.Form["data"]

...是某种数组。添加 .ToString() 只会返回它的类型名称。将它传递给反序列化是给你错误的原因,因为反序列化期望一个数组并且只得到一个字符串。

尝试关闭 .ToString() - 这可能不起作用。如果不是,则必须将 httpRequest.Form["data"] 转换为您放入其中的任何数据类型(您没有说)。

【讨论】:

  • 然后,正如我所说:“您必须将 httpRequest.Form["data"] 转换为您放入其中的任何数据类型(您没有说)。”
猜你喜欢
  • 2018-10-25
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多