From the course: ASP.NET Core in .NET 6: Dependency Injection

Unlock the full course today

Join today to access over 23,000 courses taught by industry experts.

Solution: Set up an application using dependency injection

Solution: Set up an application using dependency injection - ASP.NET Core Tutorial

From the course: ASP.NET Core in .NET 6: Dependency Injection

Solution: Set up an application using dependency injection

(upbeat music) - [Instructor] Here is one solution that displays all the products on the homepage using dependency injection and ASP.NET Core MVC. The first thing we have to do is to add the storage service and product service to the DI container. Although we are not using the storage service directly in the MVC application, the product service depends on the storage service. So it needs to be added. Afterwards, we move to our HomeController class to inject the product service as a parameter in the controller. From there, we can create a private field in the controller that will store an instance of the product service. Then it's just a case of calling the GetAll method from the private field and returning it as part of the View in the Index action. By running our application, we can see that all four of our products are displayed on the homepage of our web application.

Contents