0

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.

stack questions
  • 862
  • 2
  • 15
  • 29

1 Answers1

0

User status, it must have some code

 if (data.status=== 200) {
    this.showSnackbar('Successfully added client');
    this.resetForms();
}
Saurin Vala
  • 1,898
  • 1
  • 17
  • 23