Chapter 3. Controllers

As discussed in the first chapter, all web applications receive requests from the server and produce a response, that is delivered back to the end user. A Controller does the job of receiving the request and producing the output based on the input data in ASP.NET MVC.

In this chapter, you'll be learning about the following topics:

  • Role of the Controller in ASP.NET MVC applications
  • Routing introduction and concepts
  • Creating your first ASP.NET 5 application
  • Installation of the ASP.NET Core NuGet packages in your application
  • Creation of your first Controller and action method, which returns a simple Hello World
  • Adding a View and making the changes that allow your Controller to use that View
  • Adding a Model and passing that Model data to your View

Role of the Controller in ASP.NET MVC applications

At the high level, the Controller orchestrates between the Model and the View, and sends the output back to the user. This is also the place where authentication is usually done through action filters. Action filters will be discussed in detail in the Filters section of this chapter. The following figure illustrates the high-level flow of a request (with the steps) in ASP.Net MVC and shows us how the Controller fits into the big picture:

Role of the Controller in ASP.NET MVC applications

The following is the sequence of events that will happen at high level when the user is accessing the ASP.NET Core application:

  1. The user types the URL in the browser.
  2. Based on the pattern of the URL, the routing engine selects the appropriate Controller.
  3. The Controller talks to the Model to get any relevant data through its action methods. Action methods are methods within a controller class.
  4. The Controller then passes the data to the View to present it in a viewable format, typically as HTML elements.
  5. The View is finally delivered to the user, which he would be viewing in his browser.

Before discussing the controller, let us discuss the fundamentals of routing concepts, as the routing engine only chooses the appropriate controller and action method at runtime.

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

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