What is a controller?

Controllers are the C part of the MVC pattern. They are a set of actions that usually handle requests from a client. You should bear in mind that what we are discussing in this chapter refers to the MVC stack that's defined by ASP.NET Core. Furthermore, if we take as reference the incoming requests, remember that they have already passed through the others middleware in the middleware pipeline and that they have already hit the MVC middleware.

The following diagram shows how a request is typically handled:

As we discussed in Chapter 1, REST 101 and Getting Started with ASP.NET Core, the incoming request is usually generated by a client: the browser, another API, or an external system. The request is composed of an HTTP verb, a URIbody payload, and other additional information. The Routing engine handles the request and passes it to an action method inside one of our controllers. The Action methods usually proceed by providing a response. Furthermore, controllers usually interact with third-party systems through other classes, such as databases or other services. Finally, they serve the result in a specific format. In the case of MVC applications, they usually return a view, while in the case of the web API, they return the result in a format such as JSON/XML:

The preceding diagram shows the flow of an incoming request through the model-controller stack. As you can see, the flow is omitting the views part of the MVC stack since it is not useful for the purpose of building web services. The next part focuses on controllers and explains how to identify a controller.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.220.114