Summary
In this chapter, we learned how to implement an API controller to handle requests to a REST API endpoint. We discovered that inheriting from ControllerBase
and decorating the controller class with the ApiController
attribute gives us nice features such as automatic model validation handling and a nice set of methods for returning HTTP status codes.
We used AddScoped
to register the data repository dependency so that ASP.NET uses a single instance of it in a request/response cycle. We were then able to inject a reference to the data repository in the API controller class in its constructor.
We learned about the powerful model binding process in ASP.NET and how it maps data from an HTTP request into action method parameters. We discovered that in some cases it is desirable to use separate models for the HTTP request and the data repository because some of the data can be set on the server, and requiring less data in the request helps...