类型' System.InvalidOperationException'的例外情况发生在System.Web.Extensions.dll中但未在用户代码中处理

时间:2014-04-22 11:28:05

标签: c#

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

这部分

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

1 个答案:

答案 0 :(得分:0)

很明显地说:

的内容
httpRequest.Form["data"]

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

尝试关闭.ToString() - 这可能不起作用。如果没有,你将不得不将httpRequest.Form [“data”]转换为你放入它的数据类型(你不说)。