ASP.NET MVC Pipeline life-cycle Part 1



What is MVC ?


MVC isn’t a programming language, it’s an architecture and a way of communication between system modules.

In an MVC application the system is divided into three main modules :

  1. Model : contains system and business data
  2. View : represents a way to display the data to users.
  3. Controller : represents business logic that needs to be calculated after manipulating the data. It acts as a linkage between the Model and the View

You can see a brief graph about the MVC architecture in the below image.

Model View Controller

The HTTP request comes in. Then the controller handles the request then retrieves the data do some stuff and process those data. It Then sends the result to the view to be rendered and a response is sent back to the client.

This is MVC in a glance. We will see how the MVC works in details in the following section

ASP.NET MVC life cycle order in details:

The ASP.NET MVC Life cycle executes in the following order as shown in the image below :

  • HTTP Application
  • Routing
  • Controller
  • Authentication Filters
  • Authorization Filters
  • Authentication Challenges
  • Action Execution
  • Result Execution

ASP.NET MVC Pipeline

  • HTTP Application

    • The Application_start() event is called
    • In this stage the application initializes and the app events are registered along with ASP.NET MVC Areas, Filter and Routings.
    • This is an application-wide process
    • You can see the process if you click on the global.asax file in project file

Global Asax File

If you opened the file, you can find the registration code

  • Routing

    • Routing is the process of matching the coming request URL to the correct resource and controller
    • MVC Routing is done via Routing table that is defined using MapRoute method in Application_Start function in global.asax file we discussed earlier
    • If you navigated to Application_Start() function you can see this line inside the function body :
      • RouteConfig.RegisterRoutes(RouteTable.Routes);
    • In RouteConfig.cs file in App_Start folder you can see the code for registering the routing table

  • routes.MapRoute function accepts parameters :
    • Name : routing name
    • Url : URL pattern to be matched
    • Defaults : default values for url if they are not specified
  • You can see the routing flow in the following image

MVC Routing

In the next part, we will continue with the rest of the pipeline steps. Stay tuned.