Lifecycle of An Aspnet MVC 5 Application
Lifecycle of An Aspnet MVC 5 Application
Lifecycle of An Aspnet MVC 5 Application
This document shows the lifecycle of every ASP.NET MVC application, beginning from receiving the HTTP request from the client to sending the HTTP response back to the client. It is designed both as an education tool for those who are new to ASP.NET MVC and also as a reference for those who need to drill into specific aspects of the application.
HttpApplication Processing Pipeline
Controller Creation
Routing
Authentication and Authorization
MvcHandler
Model Binding
HTTP Response
IHttpModule.Init
The diagram shows relevant HttpApplication stages to help you understand where MVC integrates into the ASP.NET application lifecycle. In addition, for the overridable methods on the Controller object that are part the MVC lifecycle, the diagram identifies when these methods are invoked in the processing pipeline and why you might want to override them. You may or may not have the need to override any one method, but It is important for you to understand their role in the application life cycle so that you can write code at the appropriate life cycle stage for the effect you intend.
Called by the MvcHandler to create the named controller. By default, DefaultControllerFactory is used. To register a custom IControllerFactory, use ControllerBuilder.Current.SetDefaultControllerFactory in Application_Start (in Global.asax.cs) Override to perform processing before anything is done on the controller Override to perform custom initialization before ControllerBase.Initialize sets Controller.ControllerContext with the encapsulated route data.
Controller.OnAuthentication
If(AuthenticationContext.Result==null) true
false
<IAuthenticationFilter1>.OnAuthentication
If(AuthenticationContext.Result==null) true
false
<IAuthenticationFilter2>.OnAuthentication
Controller.Initialize
Match request to defined route
If(AuthenticationContext.Result==null) true
false
Controller.BeginExecuteCore
Retrieve MvcHandler as the HttpHandler for the request (stored at HttpApplication.Context.Handler)
... End
Use IAuthenticationFilter to authenticate a user action toward the intended resources. Authentication filters execute in order until one of the filters returns a non-null result.
false
HttpApplication.BeginProcessRequest
HttpApplication.BeginProcessRequest
HttpApplication.EndProcessRequest
If(AuthorizationContext.Result==null) true
Controller.OnAuthorization
HTTP Response
If(AuthorizationContext.Result==null) true
false
<IAuthorizationFilter1>.OnAuthorization
Color Legend
HttpApplication.EndProcessRequest
Invoke action with action filters (IActionFilter) Controller.EndExecute Invoke authentication challenges (IAuthenticationFilter)
If(AuthorizationContext.Result==null) true
false
HttpApplication
<IAuthorizationFilter2>.OnAuthorization
Routing
Controller
Controller.EndExecuteCore
If(AuthorizationContext.Result==null) true false
Authentication Filters
Authorization Filters Authentication Challenges Action Execution
Execute result
Execute result with result filters (IResultFilter)
... End
Result Execution
Controller.Dispose
Use IAuthorizationFilters to authorize a user action toward the intended resources. Authorization filters execute in order until one of the filters returns a non-null result.
HttpApplication.BeginProcessRequest HttpApplication.EndProcessRequest
Controller implements IResultFilter (by inheriting ActionFilterAttribute). OnResultExecuting methods are executed in order before the result is executed.
Controller.OnAuthenticationChallenge
<IAuthenticationFilter1>.OnAuthenticationChallenge
After authentication challenges are executed as a result of failed authentication or failed authorization, the result is executed immediately without any result filters.
Controller.EndExecuteCore
<Named>Controller.<Action> ... <ActionFilter2>.OnActionExecuted <ActionFilter1>.OnActionExecuted Controller.OnActionExecuted
OnActionExecuted methods are executed in reverse order after the action method itself.
OnResultExecuted methods are executed in reverse order after the result is executed