我是 ASP.NET MVC 的新手。我在 VS2012 中使用 MVC4。我得到了 id 并且一切正常。当我将 ajax 代码中的 http 请求从 get 更改为 post 时,它失败并出现 500 错误。
所以问题 1 - 我如何调试这种问题 2 - 为什么会在这个特定的实例中发生
我很确定这与我不了解我应该在路由代码中包含的内容有关
我认为很重要的代码
//the controller method
[System.Web.Mvc.HttpPost]
public void PostPatientAppointment(PatientAppointment patientAppointment)
{
init();
updatePatientAppointmentList();
foreach (PatientAppointment existingPatientAppointment in patientAppointments)
{
if (patientAppointmentConflict(existingPatientAppointment, patientAppointment ))
{
throw new HttpException(409, "Conflict");
}
}
//return patientAppointment;
}
function onMouseUp(evt) {
rect = null;
mouseDown = false;
//check for conflicting appointments
//do the xhr request
//cycle through appointments looking for conflicts
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
//open Dojo modal dialogue
alert("will open dialogue");
}
else if( xmlhttp.status == 409)
{
alert("There is a conflict with that patient appointment");
}
alert("hmm - status was: " + xmlhttp.status );
}
}
var patientAppointment = "{'identity':-1,'doctorId':-1,'patientId':-1,'patientName':null,'startTime':-1, 'endTime':-1,'location':null,'reason':null,'note':null,'recurrenceId':0,'appointmentTypeId':0,'patientShowedUp':false,'confirmed':false,'fullDayEvent':false}";
xmlhttp.open("POST", "/api/PatientAppointment/", true);
xmlhttp.send(patientAppointment);
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional}
);
}
}