从另一个参数的webapi获取StatusCode

时间:2018-06-01 06:02:56

标签: angular

我想通过以下代码从web api获取状态代码:

的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);
}

**客户**

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();
    }
    });
}

我可以获得该人的属性,但我无法获得StatusCode。你能帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:0)

用户状态,必须有一些代码

 if (data.status=== 200) {
    this.showSnackbar('Successfully added client');
    this.resetForms();
}