From the course: .NET Microservices for Azure Developers

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Obtaining a single pet in the PetsController

Obtaining a single pet in the PetsController - Azure Tutorial

From the course: .NET Microservices for Azure Developers

Obtaining a single pet in the PetsController

- [Instructor] Let's now add another endpoint for returning a single pet instead of the entire list. So here, let's return Task of IActionResult, but this time it's going to be GetById. And of course I need to use HttpGet. And this time I'm going to receive the ID of the pet, and that's why I'm going to add this parameter. And let's do the following. Let's use dbContext and of course pets. And then include, since I want to include the breed information. And then I want to pass Where and this expression. And finally FirstOrDefaultAsync. So this is going to return a single pet. Finally, let's return this with the Ok method. So now we're ready to test this out. You can see that we have both pets and pets that receives the ID. So let's try it out. And here, let's pass say two. And of course this is going to return the correct pet, including the breed data.

Contents