2

I have a c# WEB API using Entity Framework - I am trying to make a put request through my angular front end to simulate a 'checkin' function but the request isn't going through.

Here is my Web api method

   [HttpPut("checkin/{id}")]
    [AuthorizeRoles(Role.Administrator, Role.User)]
    public async Task<IActionResult> CheckIn(int id)
    {
        var reservation = await context.Reservations.FindAsync(id);
        var username = User.Identity.Name;

        if (reservation == null)
            return NotFound();

        // Ensure user is checking-in their own reservation and its not already checked-in
        if (reservation.UserName == username && reservation.CheckInDate == null)
        {
            reservation.CheckInDate = DateTime.Now;

            var count = await context.SaveChangesAsync();

            if (count < 1)
                return StatusCode((int)HttpStatusCode.InternalServerError);
        }

        return Ok();
    }

Here is my two .ts files where the request is being initiated -note: in the second method I decided to pass the ID manually for testing purposes

-checkIn(id: number){
    if (confirm('Would you like to check in')) {
     this.reservationService.checki(7);
    };
  }

reservationservice.ts

 checki(id: number) {
    const headers = new Headers();
    const url = `${this.url}+/checkin/${7}`;
    return this.http
             .put(url, JSON.stringify(7), {headers: headers})
             .map(res => res.json());
  }
rahulchawla
  • 101
  • 2
  • 9
  • Had a similar issue with PUT requests on a server recently. Turned out WebDAV was intercepting the request and generally making a pain of itself. http://www.gavincoates.com/Archive/2016/07/using-webapi-disable-webdav-to-avoid-putdelete-issues – Steve Py Jun 13 '18 at 23:13

1 Answers1

6

With Angular HTTP client, all Http requests are Observables which means you need to subscribe to them to have them called.

So in your checkin function you need to do the following:

-checkIn(id: number){
  if (confirm('Would you like to check in')) {
   this.reservationService.checki(7).subscribe(res => {});
  };
}

There is no need to handle the response explicitly.

As far as the backend handling the put I would look at this answer, I'm not a c# expert by any stretch. https://stackoverflow.com/a/32835329/8350917

  • I made the specified change and the request doesn't hang anymore - however, I don't believe the method has been executed because my DbContext is still the same - I have posted my full checkIn web api method I'd appreciate if you could check it out – rahulchawla Jun 13 '18 at 18:19