This section updates the Get methods for ASP.NET Core.
- [Instructor] Let's look at the first Get method. In prior versions of ASP.NET MVC and Web API, the name of the method determined the verb that it responded to. So, anything that began with a Get was a Get, anything that began with a post was a post, etc. This is no longer applicable. We have to specifically state the verb that we want to respond to.
And that's okay, I think it adds clarity to what we are doing. The response type no longer applies, we can just go ahead and delete that. And another change is that we never return an HttpActionResult, everything is just IActionResult. So we make that change, and then we also have to specify that it's a Get. We also need to add route parameters, and fortunately we can combine those into one attribute.
So if we put HttpGet, we can add routing right in here. So, we want customerId, and we're going to put a constraint on there that it has to be an int. And we want to name this route: GetForCustomer. Now the prior version, the route was hard-coded as API, controller, ID, but we want to be more specific, and because we have attribute routing and we're doing each method's route independently, we can.
This is technically a customerId. So we make that match the route, and then we fix the variable there. The final Get method takes in a customerId and a productId. So, let's copy Http verb and route from there. We're going to add in another route value: productId, and we'll do a constraint of int on that as well.
And we'll change this name to GetOneForCustomerAndProduct. Once again, we have to change it just to IActionResult, cleanup our variable names here. ID's just a little vague, we really want the customerId and productId. And that finishes up the Get 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