From the course: Building Web APIs with ASP.NET Core 8

Unlock the full course today

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

Solution: Searching items

Solution: Searching items

(upbeat music) - [Instructor] Possibly the hardest part of this challenge was to find a good URI. I would recommend /api/products/available. So all of the available products will be returned, and if you would like to go down that route, no pun intended, then here's how this can be implemented. So we use HttpGet and our route is now available. And then we have public async Task. And then, very similar to get all products, we can use ActionResult or ActionResult of IEnumerable of Product. Both will work. GetAvailableProducts. And then, essentially, we return await, and then _context.Products.Where, product IsAvailable is true. And then we converted to an array in an asynchronous fashion. Basically that's all we need to do. And if we'd like to work with a minimal API here, that, of course, works as well. So app.MapGet, and then full URI, products/available. And it's an async method that only needs the ShopContext coming in via dependency injection. And then, what we are returning is…

Contents