MVC Life Cycles

At a high level, life cycle is a sequence of steps or events used to handle some type of request or change of an application state.

1. Application life cycle – refers to the time when the application process starts to run in IIS (Application start) until the process stops (Application end).

2. MVC Request life cycle – the sequence of events that happens every time an HTTP request is handled by the application.

MVC Request life cycle MVC Request Life Cycle MVC Request Life Cycle

Routing: The entry point for every MVC Application begins with Routing. After the application receives the request, it uses URL Routing Module to handle the request. The routing module is responsible for matching the incoming urls with the routes defined in the application. All routes have an associated MVC Route Handler. If the request is matched with one of the routes defined into application, the MVC Route Handler executes and retrieves an instance of MVC HttpHandler.

Controller initialization: The MVC HttpHandler begins the process of initializing and executing a controller. MVC framework converts our routing data into a specific controller that handle the request. This is accomplished by Controller Factory and Activator. This is the step where dependency injection are resolved. The next major step after controller creation is action execution.

Action execution: A component called Action Invoker finds and selects an appropriate Action Method to invoke in our controller. Before the method is called, Model Binding takes place and it maps the data from HttpRequest to parameters into our Action Methods. Action Filters are called before and after the method creates an Action Result.

Result execution: MVC separates declaring of the result from executing the result. If the result is a View type, the View Engine is called and it’s responsible for finding and rendering our view. If the result it’s not a view, the ActionResult executes on it’s own.

HttpModules and HttpHandlers

From the developers point of view the difference between those are: one implements IHttpModule interface another implements IHttpHandler interface.

Module participates in the request processing of every request in order to change or add to it in some way.

Handler is responsible for handling the request and producing the response for specific content types.