Web API不会将json post反序列化为EF类型

时间:2013-07-10 21:25:41

标签: asp.net-web-api

昨天我有一个有效的Web API实现。我今天进来,我的帖子参数不再反序列化为我的实体对象

这是我的api控制器代码:

    public class DocBooksMDController : ApiController
    {

        private BoxEntities db = new BoxEntities();

        // POST api/DocBooksMD/ack
        [HttpPost]
        [ActionName("Acknowledge")]
        public HttpResponseMessage MarkMessageAcknowledged(DocBooksMDMessage messageIn)
        {
            if (ModelState.IsValid)
            {
                // retrieve docBooksMDMessage
                var query = from messages in db.DocBooksMDMessages
                            where messages.MessageControlID == messageIn.MessageControlID
                            select messages;
                DocBooksMDMessage messageOut = query.FirstOrDefault();
                if (messageOut == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Invalid Message Control ID"));
                }

                // Set Acknowledgement and Feedback
                messageOut.DateAcknowledged = DateTime.Now;
                messageOut.DocBooksFeedback = messageIn.DocBooksFeedback;
                db.SaveChanges();

                // send response
                return Request.CreateResponse(HttpStatusCode.OK, messageOut);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/DocBooksMD/read
        [HttpPost]
        [ActionName("Read")]
        public HttpResponseMessage MarkMessageRead(DocBooksMDMessage messageIn)
        {
            if (ModelState.IsValid)
            {
                // retrieve docBooksMDMessage
                var query = from messages in db.DocBooksMDMessages
                            where messages.MessageControlID == messageIn.MessageControlID
                            select messages;
                DocBooksMDMessage messageOut = query.FirstOrDefault();
                if (messageOut == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Invalid Message Control ID"));
                }

                // Set Read and Feedback
                if (messageIn.DateRead == null) messageIn.DateRead = DateTime.Now;
                messageOut.DateRead = DateTime.Now;
                messageOut.DocBooksFeedback = messageIn.DocBooksFeedback;
                db.SaveChanges();

                // send response
                return Request.CreateResponse(HttpStatusCode.OK, messageOut);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

这是我的实体类:

public partial class DocBooksMDMessage
{
    public int ID { get; set; }
    public int ExamSourceID { get; set; }
    public string PatientLastName { get; set; }
    public string PatientFirstName { get; set; }
    public string PatientMRN { get; set; }
    public string ExamAccession { get; set; }
    public string ExamCode { get; set; }
    public string ExamDescription { get; set; }
    public string ReferringProviderCode { get; set; }
    public string ReferringProviderNPI { get; set; }
    public string ReferringProviderLastName { get; set; }
    public string ReferringProviderFirstName { get; set; }
    public string Message { get; set; }
    public string MessageControlID { get; set; }
    public System.DateTime DateSent { get; set; }
    public Nullable<System.DateTime> DateAcknowledged { get; set; }
    public Nullable<System.DateTime> DateRead { get; set; }
    public string DocBooksFeedback { get; set; }
}

这是昨天工作的json请求,但今天不起作用:

Content-Type: application/json
Host: tbd:8080
Content-Length: 127
{
  "MessageControlID":"65991775",
  "DateAcknowledged":"2013-07-09T16:09:13.1564342-05:00",
  "DocBooksFeedback":"Test Feedback"
}

我真的不知道如何继续。

这表明该方法正在获得预期的请求体:

string body = Request.Content.ReadAsStringAsync().Result;

1 个答案:

答案 0 :(得分:0)

好的 - 这是正式的。我是个白痴。当我在fiddler工作时,我将内容类型从请求标题中删除了。反序列化现在运行正常:

Content-Type: application/json