The request life cycle, from start to end
If you come from ASP.NET Web Forms you may know the page life cycle. ASP.NET Core MVC has no page events; instead every request follows a request life cycle. This lesson traces one request from the moment the browser sends it to the moment HTML comes back.
- StartBrowser sends a requestFor example GET /products/details/5
- Step 2Kestrel receives itThe web server builds an HttpContext with the request and an empty response
- Step 3Early middleware runsException handling, HTTPS redirection, static files
- Step 4Routing picks an endpointThe URL is matched to ProductsController.Details with id = 5
- Step 5Authentication and authorizationWho is the user, and may they reach this endpoint?
- Step 6Controller is createdDependency injection supplies its services
- Step 7Model binding and validationValues from the route, query string or form fill the action parameters
- Step 8Filters and the action runFilters wrap the action; the action returns an IActionResult
- Step 9Result is executedFor a view, Razor renders HTML inside the layout
- EndBrowser gets the responseThe response passes back out through the middleware
Steps 6 to 9 belong to MVC. Everything else is the ASP.NET Core pipeline that every kind of app shares.
The idea in plain words
HTTP is a request and response protocol: the browser asks, the server answers, and the connection forgets about it. In ASP.NET Core every request gets its own HttpContext object that carries the request (URL, headers, cookies, body) and the response (status code, headers, body) through the whole pipeline.
Think of the pipeline as a series of checkpoints. Each piece of middleware can inspect the request, stop it (for example redirect to HTTPS or return a static file), or pass it on. When the request reaches the endpoint, MVC takes over: it creates the controller, binds the model, runs filters and the action, and executes the result. Then the response travels back out through the same middleware in reverse order.
Page life cycle vs request life cycle
Web Forms pages raised events such as Init, Load, PreRender and Unload, and kept state in ViewState. MVC does not use these. If an interviewer asks about the page life cycle in ASP.NET Core MVC, describe the request life cycle and the filter pipeline instead.
- Code that ran in Page_Load usually belongs in the controller action.
- Code that ran for every page belongs in middleware or a filter.
- State is not kept automatically between requests; see the state management module.
Example: watch the start and end of a request
This inline middleware, placed early in Program.cs, logs when a request starts and when it ends. Everything after await next() runs on the way back out, after the controller and view have finished.
app.Use(async (context, next) =>
{
var watch = System.Diagnostics.Stopwatch.StartNew();
app.Logger.LogInformation("Start: {Method} {Path}",
context.Request.Method, context.Request.Path);
await next(); // routing, the controller and the view all run here
app.Logger.LogInformation("End: {Status} in {Ms} ms",
context.Response.StatusCode, watch.ElapsedMilliseconds);
});Common mistakes
Knowing the order of the life cycle explains many confusing bugs.
- Adding UseAuthorization before UseRouting or UseAuthentication, so authorization has nothing to work with.
- Expecting fields on a controller to keep values between requests. A new controller is created for every request.
- Changing response headers after the response has started sending, which throws an exception.
Interview question
Explain the request life cycle of an ASP.NET Core MVC application.
Kestrel receives the request and creates an HttpContext. The request passes through the middleware pipeline in the order configured in Program.cs. Routing matches the URL to a controller action endpoint. MVC creates the controller through dependency injection, performs model binding and validation, then runs authorization, resource and action filters around the action. The action returns an IActionResult, result filters run, and the result is executed, for example a Razor view rendered to HTML. The response then flows back through the middleware to the client.
Want the full details? Read the official Microsoft documentation for this topic.