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

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…

Contents