From the course: Building Web APIs with ASP.NET Core 8
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Adding an item with POST
From the course: Building Web APIs with ASP.NET Core 8
Adding an item with POST
- [Instructor] We are now ready to add a new item. In our application, we could add a new product to our data store. First of all, we need an action method. That action method needs the HttpPost attribute because that method will be responsible for answering HttpPost requests. Remember post is the HTTP method we will be using to add new content. The name of that method will be PostProduct. As usual, we start the name of the method with the HTTP method we are using. And the argument for that method is of type product. That's the model binding we've just talked about. ASP.NET Core web API assembles a product out of the individual values that are sent as part of the HTTP request as you'll see in a minute. In the implementation of that method, we have to access our database context, so DB or _context to, we were calling that and then .Products, which represents all of the products in our data store .Add. And then we can add the product. That's what Entity Framework Core does. So that's…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
- (Locked)HTTP methods3m 8s
- (Locked)Model binding4m 31s
- (Locked)Adding an item with POST12m 14s
- (Locked)Model validation5m
- (Locked)Updating an item with PUT7m 59s
- (Locked)Deleting an item with DELETE4m 45s
- (Locked)Migrating the code to Minimal APIs6m 21s
- (Locked)Challenge: Deleting several items41s
- (Locked)Solution: Deleting several items5m 40s
- (Locked)