Explain the ASP.NET MVC request life cycle.

ASP.NET MVC Fundamentals Intermediate 2–3 Yrs3–5 Yrs Conceptual 2 min read

Quick Answer

The request is matched by routing to a controller and action, the controller factory creates the controller, the action invoker runs authorization and action filters, performs model binding and validation, executes the action method, gets back an ActionResult, runs result filters, and the result executes — a view result renders Razor to HTML which is written to the response.

First, routing takes the URL and works out which controller and action to call. Then the controller is created — through DI in Core. Before the action runs, the framework runs authorization filters, does model binding to fill the action parameters from the request, runs validation into ModelState, and then runs any action filters. The action method executes and returns an ActionResult. After that, result filters run and then the result executes — for a ViewResult that means the Razor engine renders the view with the model into HTML and writes it to the response. Exception filters sit around the action and result steps. In ASP.NET Core all of this happens inside the endpoint middleware, so the middleware pipeline runs around the outside.

In order:

  1. Routing — the URL is matched against the route table to produce route values, including controller and action.
  2. Controller creation — the controller factory (backed by DI in Core) instantiates the controller.
  3. Authorization filters — [Authorize] and friends run first; they can short-circuit with a 401/403.
  4. Model binding — route values, query string, form body and headers are mapped onto the action's parameters.
  5. Validation — data annotations and IValidatableObject populate ModelState.
  6. Action filters — OnActionExecuting before, OnActionExecuted after.
  7. Action execution — your method runs and returns an IActionResult.
  8. Result filters — OnResultExecuting / OnResultExecuted.
  9. Result execution — ViewResult locates the view, runs the Razor view engine with the model, and writes HTML; JsonResult serialises; RedirectResult sends a 302; etc.
  10. Exception filters wrap steps 6–9 and can convert an exception into a result.

In ASP.NET Core this whole sequence sits inside the endpoint middleware, so the middleware pipeline (routing, auth, etc.) runs around it.