Configuring the Customer Repository and Web API Controller
Explore how to set up and configure a customer repository within an ASP.NET Core Web API controller. Understand how to register scoped dependencies, apply constructor injection, and define routes for CRUD operations on customer data. This lesson guides you through creating a controller that handles common actions like retrieving, creating, updating, and deleting customers, while using modern practices such as automatic model validation.
We'll cover the following...
Now we will configure the repository to be called from within a Web API controller.
Defining a Web API controller for working with customer
We will register a scoped dependency service implementation for the repository when the web service starts up and then use constructor parameter injection to get it in a new Web API controller for working with customers. To show an example of differentiating between MVC and Web API controllers using routes, we will use the common /api URL prefix convention for the customer’s controller:
Step 1: In Program.cs, import the namespace for working with our customer’s repository, as shown in the following code:
Step 2: In Program.cs ...