This section updates the Post method for ASP.NET Core.
- [Instructor] Now it's time to migrate the post method. So we're going to get rid of the ResponseType, replace that with an HttpPost. Now we don't have to add any route information because api/ShoppingCart is what we want. Remember our post is an ad, so we leave the from body to get the shopping cart record, we have to change this HttpActionResult just to ActionResult.
And another thing that we can eliminate is this generic try catch. Because we have the exception filter built in. Now if we were doing specific error handling in this method, which we should do in a production application, we would have a try catch, but we have the generic one, so we can remove that. The other changes we need to make are the CreatedAtRoutes. So if a shopping cart record on add resulted in not adding, for example, as we discussed in the data access layer, if you add a record that's already in the cart, and the quantity becomes zero or less than zero, that item gets removed from the cart.
So in that case, we want to return the header, CreatedAtRoute of GetForCustomer, and remember we changed the id to the more meaningful CustomerId. So we have to change that as the route parameter. And the rest of the line can stay the same. If a record was successfully added, we want to return that specific route for that record and we change the name of that to GetOneForCustomerAndProduct.
And we have to change the parameter name to customerId and the rest of the line can stay the same. And that completes our changes for the post method.
- 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