类型';System.InvalidOperationException';发生在System.Web.Ex

本文关键字:System Web Ex InvalidOperationException 类型 | 更新日期: 2023-09-27 17:59:24

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

该部分

           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;
}

类型';System.InvalidOperationException';发生在System.Web.Ex

这很清楚地表明:的内容

httpRequest.Form["data"]

是某种类型的数组。添加.ToString()只会返回其类型名称。将其传递给反序列化会导致错误,因为反序列化需要一个数组,而只得到一个字符串。

尝试关闭.ToString()-这可能不起作用。如果没有,你就必须将httpRequest.Form["data"]转换为你输入的数据类型(尽管你没有说)。