web api - 发布对象返回内部服务器错误500(web api - posting object returns internal server error 500)

我有一个简单的web api,它使用数据库的第一个ADO SQL服务器实体,其中一个表具有自动递增标识列和nvarchar列。 我的GET功能运行正常,但我遇到POST问题。

这是我的控制器,它主要是自动生成的:

public class CustomersController : ApiController { private MyEntities db = new MyEntities(); // GET: api/Customers public IQueryable<Customer> GetCustomers() { return db.Customers; } // GET: api/Customers/5 [ResponseType(typeof(Customer))] public IHttpActionResult GetCustomer(int id) { Customer customer = db.Customers.FirstOrDefault(x => x.CustomerPk == id); if (customer == null) { return NotFound(); } return Ok(customer); } // PUT: api/Customers/5 [ResponseType(typeof(void))] public IHttpActionResult PutCustomer(int id, Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != customer.CustomerPk) { return BadRequest(); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); } // POST: api/Customers [ResponseType(typeof(Customer))] public IHttpActionResult PostCustomer(Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Customers.Add(customer); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = customer.CustomerPk }, customer); } // DELETE: api/Customers/5 [ResponseType(typeof(Customer))] public IHttpActionResult DeleteCustomer(int id) { Customer customer = db.Customers.FirstOrDefault(x => x.CustomerPk == id); if (customer == null) { return NotFound(); } db.Customers.Remove(customer); db.SaveChanges(); return Ok(customer); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool CustomerExists(int id) { return db.Customers.Count(e => e.CustomerPk == id) > 0; } }

在HTML页面中,我调用以下jquery ajax:

var customer = { "CustomerPk": 0, "CustomerName": "TEST2" }; $.ajax( { url: "http://serverurl/site/api/customers", type: "POST", contentType: "application/json; charset=UTF-8", data: JSON.stringify(customer), dataType: "json", success: function (data, textStatus, xhr) { alert("Success"); }, error: function (xhr, textStatus, errorThrown) { var msg = "Code: " + xhr.status + "\n"; msg += "Text: " + xhr.statusText + "\n"; if (xhr.responseJSON != null) { msg += "JSON Message: " + xhr.responseJSON.Message + "\n"; } alert(msg); } });

就像我提到的,当我从这个页面调用GET方法时,一切正常。 我已经尝试了基于无数的将对象发布到web api在线的例子的ajax方法的不同变体,主要是更改我发布的对象,省略了标识列,只是发送了CustomerName列,或者将Pk设置为null ,但没有一个工作。 我总是得到500内部服务器错误。

它不适用于自动递增标识列吗? 我见过的例子似乎没有那些作为模型的一部分,所以我想知道这是不是我的问题。

非常感谢任何帮助或建议,谢谢。

I have a simple web api that uses a database first ADO SQL server entity, with one table in it that has an auto-incrementing identity column and a nvarchar column. My GET functions work fine, but I'm having trouble with POST.

Here is my controller, which was largely automatically generated:

public class CustomersController : ApiController { private MyEntities db = new MyEntities(); // GET: api/Customers public IQueryable<Customer> GetCustomers() { return db.Customers; } // GET: api/Customers/5 [ResponseType(typeof(Customer))] public IHttpActionResult GetCustomer(int id) { Customer customer = db.Customers.FirstOrDefault(x => x.CustomerPk == id); if (customer == null) { return NotFound(); } return Ok(customer); } // PUT: api/Customers/5 [ResponseType(typeof(void))] public IHttpActionResult PutCustomer(int id, Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != customer.CustomerPk) { return BadRequest(); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); } // POST: api/Customers [ResponseType(typeof(Customer))] public IHttpActionResult PostCustomer(Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Customers.Add(customer); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = customer.CustomerPk }, customer); } // DELETE: api/Customers/5 [ResponseType(typeof(Customer))] public IHttpActionResult DeleteCustomer(int id) { Customer customer = db.Customers.FirstOrDefault(x => x.CustomerPk == id); if (customer == null) { return NotFound(); } db.Customers.Remove(customer); db.SaveChanges(); return Ok(customer); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool CustomerExists(int id) { return db.Customers.Count(e => e.CustomerPk == id) > 0; } }

In an HTML page I am calling the following jquery ajax:

var customer = { "CustomerPk": 0, "CustomerName": "TEST2" }; $.ajax( { url: "http://serverurl/site/api/customers", type: "POST", contentType: "application/json; charset=UTF-8", data: JSON.stringify(customer), dataType: "json", success: function (data, textStatus, xhr) { alert("Success"); }, error: function (xhr, textStatus, errorThrown) { var msg = "Code: " + xhr.status + "\n"; msg += "Text: " + xhr.statusText + "\n"; if (xhr.responseJSON != null) { msg += "JSON Message: " + xhr.responseJSON.Message + "\n"; } alert(msg); } });

Like I mentioned, when I call the GET methods from this page, everything works. I've tried different variations of the ajax method based on countless examples of posting an object to web api online, mostly changing the object I'm posting, leaving out the identity column and just sending the CustomerName column, or setting the Pk to null, but none of those worked. I always get a 500 internal server error.

Does it not work with auto-incrementing identity columns? The examples I've seen don't seem to have those as part of the model, so I'm wondering if that's my problem.

Any help or advice would be greatly appreciated, thank you.

最满意答案

您是否可以访问任何服务器端错误日志,因为500内部错误可能有多种原因。

Do you have access to any server-side error-logs since a 500 internal error can have many causes.

更多推荐