Reputation: 409
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.
Upvotes: 0
Views: 1071
Reputation: 1550
Do you have access to any server-side error-logs since a 500 internal error can have many causes.
Upvotes: 1