Creating the Web API controller

In the Controllers folder, add a new class named CustomersController.cs.

We could delete the ValuesController.cs file, but it is good to have a simple Web API controller with minimal dependencies in a service for testing purposes.

In the CustomersController class, add the following code, and note the following:

  • The controller class registers a route that starts with api and includes the name of the controller, that is, api/customers.
  • The constructor uses dependency injection to get the registered repository for customers.
  • There are five methods to perform CRUD operations on customers—two GET methods (all customers or one customer), POST (create), PUT (update), and DELETE.
  • GetCustomers can have a string parameter ...

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.