13 Middleware, HTTP routing, and HTTP responses

 

This chapter covers

  • Routing HTTP requests to controllers and endpoints
  • Declaring HTTP routes with HttpAttribute method attributes
  • Injecting dependencies with middleware
  • Using the IActionResult interface to return HTTP responses

We are almost at the end of our journey. In chapters gone by, we implemented the database access layer, the repository layer, and the service layer. Our service is almost implemented, but not yet usable by FlyTomorrow (our client). To interact with our service, we need to provide controllers that accept HTTP requests and kick off the necessary processing.

In section 13.1, we discuss the controller’s place within our Repository/Service architecture. Following that, in section 13.2, we determine which controllers we need to implement. In the following sections, we start to implement the FlightController (section 13.3) and explore how to route HTTP requests to our endpoints (section 13.4).

Figure 13.1 In the previous chapters we implemented the database access, repository, and service layers. In this chapter, we start to implement all controllers needed for our service.

After this chapter, we are only one more chapter away from having a fully implemented service that adheres to the API specification given to us by FlyTomorrow. In the next chapter, we finish up by wrapping up our controllers and diving into acceptance testing with Swagger so we can prove we did our work correctly.

13.1   The controller class within the Repository/Service pattern

13.2   Determining what controllers to implement

13.3   Implementing the FlightController

13.3.1   Returning HTTP responses with the IActionResult interface (GetFlights)

13.3.2   Injecting dependencies into a controller using middleware

13.3.3   Implementing the GET /Flight/{FlightNumber} endpoint

13.4   Routing HTTP requests to controllers and methods

13.5   Exercises

13.6   Summary