昨天我有一个有效的 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;