I have a POST
API that looks like so:
[Route("api/Review/post")]
[HttpPost]
[AcceptVerbs("POST")]
// POST: api/Review
[ResponseType(typeof(review))]
public IHttpActionResult Postreview(review review)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.review.Add(review);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (reviewExists(review.rating_id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = review.rating_id }, review);
}
On a form, I have a button that is drawing from the fields on the form to run through the POST
API. It looks like this:
protected void Button1_Click(object sender, EventArgs e)
{
void Add_Review(int ap_id, int usr, string val, DateTime revdate, double scr, string com)
{
if (ap_id == 0)
{
throw new ArgumentException(message: "Please select a site");
}
if (string.IsNullOrEmpty(com))
{
throw new ArgumentException(message: "Please add a comment.");
}
using (var client = new HttpClient())
{
review r = new review
{
site_id = ap_id,
usr_id = usr,
valid = val,
review_date = revdate,
score = scr,
comments = com
};
client.BaseAddress = new Uri("http://localhost:#####/");
var response = client.PostAsJsonAsync("api/Review/post", r).Result;
if (response.IsSuccessStatusCode)
{
Console.Write("Success");
Response.Redirect("ReviewSubmitted.aspx");
}
else
Console.Write("Error");
}
}
int usrid;
usrid = Convert.ToInt32(ConfigurationManager.AppSettings["TestUserID"]);
Add_Review(Convert.ToInt16(AirportList.SelectedValue),
usrid,
"Y",
Convert.ToDateTime(DateTime.Now.ToString("M/d/yyyy")),
Convert.ToDouble(textScore.Text),
textComments.Text);
}
When I click the button, I would expect that it returns "Success" and redirects me to my ReviewSubmitted.aspx
page. However, even though the POST
runs successfully, when it moves back to the button_click
method, it tells me that I have a 500 error.
I have verified in the database that the POST was successful. This IS just a personal project that's never going to go anywhere, so I could just tell it to redirect on failure too, but I don't want to do that for obvious reasons.
What am I missing here? Why is it POST
ing, but still saying that there was an error?