I would like to get the status code from a web api with this code:
webapi
public HttpResponseMessage AddPerson(Person person)
{
var httpResponse = new HttpResponseMessage();
var p = new Person();
using (var db = new ApplicationDbContext())
{
p = person;
try
{
var context = db.Database.BeginTransaction();
p.DatOfBirth = Convert.ToDateTime(person.DatOfBirth);
db.Persons.Add(p);
db.SaveChanges();
context.Commit();
httpResponse.StatusCode = HttpStatusCode.OK;
}
catch(Exception ex)
{
httpResponse.StatusCode = HttpStatusCode.NotAcceptable;
httpResponse.ReasonPhrase = "Error in saving to database: " + ex.Message;
return Request.CreateResponse(httpResponse);
}
}
return Request.CreateResponse(httpResponse.StatusCode, p);
}
** client **
addPerson() {
this.clientService.addPerson(this.createClientForm.value)
.subscribe((data: any) => {
console.log(data.status);
debugger;
if (data.StatusCode === 200) {
this.showSnackbar('Successfully added client');
this.resetForms();
}
});
}
I can get the properties of the person but I cannot get the StatusCode. Can you help please. Thank you.