Request Lifecycle

This is the sequence of events that happens every time the application receives an HTTP request.

 

Routing:

  • The process begins with the Routing module.
  • The Routing module uses URL rewriting to map the incoming request's URL to a specific route defined in the application's route table (usually in RouteConfig.cs).
  • A route essentially defines a pattern for URLs and specifies which controller and action method should handle requests that match that pattern.
  • For example, a route might define that a request to "/Products/Details/5" should be handled by the Details action method of the ProductsController, with the "5" being an ID parameter.

 

Controller Instantiation:

  • Once a matching route is found, the MvcHandler is invoked.
  • The MvcHandler uses a ControllerFactory to create an instance of the appropriate controller class.
  • The ControllerFactory is responsible for actually creating the controller object. This design allows for flexibility, such as using dependency injection to create controllers.
  •  

Action Invocation:

  • The controller receives the request.
  • The ActionInvoker then determines which action method within the controller should be executed, based on the route data and possibly other factors like HTTP verb (GET, POST, etc.).
  • Action Invokers handle the process of selecting the correct method to call.

 

Model Binding:

  • Before the action method is executed, the MVC framework performs model binding.
  • Model binding maps data from the HTTP request (such as form data, query string parameters, or route data) to the parameters of the action method.
  • For instance, if a user submits a form with fields "Name" and "Email", model binding will automatically try to map those values to the Name and Email parameters of the controller's action method.

 

Action Execution:

  • The selected action method is executed.
  • The action method contains the core logic for handling the request.
  • It interacts with the Model to retrieve or update data, and it determines what data to pass to the View.
  • This is where you write the code that processes the user's request, interacts with your data, and prepares the application's response.

 

View Engine:

  • The controller action returns an Action Result.
  • If the result is a View, the View Engine is involved.
  • The View Engine locates and processes the appropriate View file (e.g., a .cshtml file in ASP.NET MVC).
  • The View is rendered, combining the data from the Model with HTML markup to generate the HTML that will be sent to the client.
  • The View Engine takes the data you pass from the controller and merges it with your View template to produce the final HTML.

 

  • Response:
  • Finally, the MVC framework sends the generated HTML (or other content, such as JSON or a file) back to the user's browser as the HTTP response.
  • The server sends this response back to the client that made the request.

Related Question