This section updates the namespaces and the base class, and constructor for ASP.NET Core.
- [Instructor] The final step is to migrate the controllers. So let's drag the ShoppingCartController from the Web API 2.2 project. Move it right into the Controller's folder, And open it up. And right off the bat we know that we have to get rid of System.Data.Entity. And that's going to be Microsoft.EntityFrameworkCore.
System.Web is no longer part of the .netFramework in .NET Core. So we know we can get rid of those. Got to do a little namespace clean up, because I did not name them the same. Shame on me. So it's SpyStoreAPI. And then the final big change from a Framework perspective, in ASP.NET MVC and Web API, we had Controllers, async Controllers, and API Controllers. In ASP.NET MVC Core, we just have Controllers.
So we now need to add some additional namespaces to support that. And that's Microsoft.AspNetCore.Mvc, as seen on line eight. We know we're also going to need to add in the interface namespace. So copy line 10, and make that SpyStoreDAL.Repos.Interfaces. And that should carry us through the rest of the Controller from a namespace perspective.
In the Web API 2.2 version, we were instantiating a new ShoppingCartRepo every time a Controller was created. We don't want to do that. We want to take advantage of the DependencyInjection. So all we need to do, because we've added the ShoppingCartRepo into the DI container, is at a parameter of IShoppingCartRepo into the Constructor, and then assign the local variable to that parameter that gets passed in.
We also need to change the type to an IShoppingCartRepo. Now I mentioned earlier in the configuration section, that we are no longer using a routing table in ASP.NET Core Web Services, but we're using attribute routing. And we want to set a whole Controller attribute. So that will be Route ... And just like it was in the prior version ... It's going to be api/ShoppingCart.
So this is the base route we're starting from. There's another attribute we can add that informs the content type of all of the methods on the Controller. This can also be set at a global level, but we know that we're going to be producing json from all of our methods.
- Creating the .NET Core project
- Adding Entity Framework
- Migrating the data access layer (DAL)
- Configuring services and the HTTP pipeline
- Adding remaining services to the dependency injection container
- Migrating controllers and actions
- Testing the services