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
From the course: Building Web APIs with ASP.NET Core 8
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Controllers and actions7m 50s
-
(Locked)
Routing basics6m 14s
-
(Locked)
Creating a data model5m 54s
-
(Locked)
Using entity framework core7m 28s
-
(Locked)
Returning a list of items4m 4s
-
(Locked)
Using ActionResult2m 44s
-
(Locked)
Returning an item4m 29s
-
(Locked)
Handling errors3m 5s
-
(Locked)
Making the API asynchronous2m 51s
-
(Locked)
Working with minimal APIs5m 22s
-
(Locked)
Challenge: Searching items31s
-
(Locked)
Solution: Searching items3m 1s
-
(Locked)
-
-