从'replacementFallback'获取值时出错

时间:2013-11-25 22:08:15

标签: json asp.net-mvc-4 asp.net-web-api json.net

当我尝试通过webapi执行PUT时,我收到以下错误。我错过了什么?为什么我会得到这个例外?

Inner Exception : {"Error getting value from 'replacementFallback' on 'System.Text.EncoderReplacementFallback'."} 
Message         : Error getting value from 'replacementFallback' 

Source          : Newtonsoft.Json

Stack Trace     :    at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
     at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext)

从C#调用WebAPI的代码

  EmailReportVM mailMsgRpt = new EmailReportVM();
        using (HttpClient client = new HttpClient())
                          {
               // Add an Accept header for JSON format
               client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
               response = client.PutAsJsonAsync(InsertMailApiURL, mailMsgRpt).Result;
// The above line I am getting the error.
                            }

EmailReportVM.cS

 public class EmailReportVM 
    {
          #region Constructors and Methods
        public EmailReportVM ()
        {
            PhotoUrl = ConfigurationManager.AppSettings["PhotoUrl"];
        }
        #endregion

        #region Properties and Fields
        private string _from;
        private string _to;
        public string ReportName { get; set; }
        public string ReportUrl { get; set; }
        public string From
        {
            get
            {
                    // remove the last comma
                    if ((!string.IsNullOrEmpty(_from)) && (_from.Length > 1))
                    {
                        if (_from.Trim().Substring(_from.Length - 1, 1).Equals(","))
                            _from = _from.Trim().Substring(0, _from.Length - 1);
                    }
                    return _from;
            }
            set {
                    _from = value;
            }
        }
        public string To { 
            get {
                    // remove the last comma
                    if ((!string.IsNullOrEmpty(_to)) && (_to.Length > 1))
                    {
                        _to = _to.Replace("\n", String.Empty);
                        if (_to.Trim().Substring(_to.Length - 1, 1).Equals(","))
                            _to = _to.Trim().Substring(0, _to.Length - 1);
                    }
                    return _to;
            } 
            set {
                    _to = value;
            } 
        }
        public string Message { get; set; }
        public long StatsEmailedId { get; set; }
        public bool CopyMeBox { get; set; }
        public string AgentId { get; set; }
        public string AgentEmail { get; set; }
        public string AgentPhone { get; set; }
        public string AgentFax { get; set; }
        private string _agentWebsite;
        public string AgentWebsite
        {
            get {

                if ((_agentWebsite != null)&& (_agentWebsite.Length > 0) && (!_agentWebsite.Equals("http://")))
                {
                    return _agentWebsite;
                }
                else
                {
                    return null;
                }
            }
            set { _agentWebsite = value; }
        }

        public bool HasAgentPhoto { get; set; }
        public string AgentName { get; set; }
        public string AgentPhotoUrl { 
            get {
                if (this.HasAgentPhoto)
                {
                    return PhotoUrl + this.AgentId + ".jpg";
                }
                else {
                    return null;
                }
            }
        }
        public string PhotoUrl { get; set; }
        public string ReportTitle { get; set; }
        public MailMessage mailMessage { get; set; }

        #endregion
    }

WebAPI中的操作

 [System.Web.Http.HttpPut]
        public object InsertMailSendQueue([FromBody] EmailReportApiVM mailMessage) 
        {
            var confirmation = EmailService.InsertMailSendQueue(mailMessage.AgentId, null, mailMessage.mailMessage, mailMessage.CopyMeBox);
            return confirmation;
        }     

0 个答案:

没有答案