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 - 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
-
-
-
-
-
(Locked)
How to inject services into a controller3m 35s
-
(Locked)
Inject services in other components of a web application3m 40s
-
(Locked)
How to set up a hosted service3m 10s
-
(Locked)
Disposing of a service2m 37s
-
(Locked)
Challenge: Set up an application using dependency injection1m 17s
-
(Locked)
Solution: Set up an application using dependency injection56s
-
(Locked)
-
-