You are on page 1of 2

ASP.

NET MVC 5 APPLICATION LIFECYCLE – HIGH-LEVEL VIEW


HttpApplication This document shows the lifecycle of every ASP.NET MVC application, beginning from receiving the
Processing Pipeline
HTTP request from the client to sending the HTTP response back to the client. It is designed both as an
HTTP Request 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 Action Invocation
HTTP Response
Action Method (with Filters)
Invocation

Result Execution
(View, etc.)
ASP.NET MVC 5 APPLICATION LIFECYCLE – DETAIL VIEW
The diagram shows relevant HttpApplication stages to help you
understand where MVC integrates into the ASP.NET application Invoke authentication filters
HttpApplication lifecycle. In addition, for the overridable methods on the Controller (IAuthenticationFilter)
Processing Pipeline object that are part the MVC lifecycle, the diagram identifies when Start

HTTP Request Tasks to perform: these methods are invoked in the processing pipeline and why you
 Common tasks such as register routes, bundles, and filters might want to override them. You may or may not have the need to Controller.OnAuthentication
Application_Start (in Global.asax.cs)  Advance tasks such as set custom controller factory
override any one method, but It is important for you to understand
HttpApplication.Init
Override to subscribe to HttpApplication events their role in the application life cycle so that you can write code at If(AuthenticationContext.Result==null) false

HttpApplication the appropriate life cycle stage for the effect you intend. true
IHttpModule.Init
Processing Pipeline <IAuthenticationFilter1>.OnAuthentication

Called by the MvcHandler to create the named controller. By default,


IControllerFactory.CreateController
DefaultControllerFactory is used. To register a custom IControllerFactory, use If(AuthenticationContext.Result==null) false
ControllerBuilder.Current.SetDefaultControllerFactory in Application_Start (in
<Named>Controller.ctor true
Global.asax.cs)
HttpApplication.PostResolveRequestCache <IAuthenticationFilter2>.OnAuthentication
Controller.BeginExecute Override to perform processing before anything is done on the controller
UrlRoutingModule.PostResolveRequestCache
Override to perform custom initialization before ControllerBase.Initialize sets
Controller.Initialize If(AuthenticationContext.Result==null) false
Controller.ControllerContext with the encapsulated route data.
Match request to defined route
Controller.BeginExecuteCore true

...
Retrieve MvcHandler as the HttpHandler Override to use a custom TempData provider. SessionStateTempDataProvider
Controller.CreateTempDataProvider
for the request (stored at is used by default.
HttpApplication.Context.Handler) Override to use a custom action invoker. AsyncControllerActionInvoker is used End
Controller.CreateActionInvoker
by default.

Invoke authentication filters


Store the encapsulated route data (at Use IAuthenticationFilter to authenticate a user action toward
HttpApplication.Context.Request (IAuthenticationFilter)
the intended resources. Authentication filters execute in order
.RequestContext)
until one of the filters returns a non-null result.
If(AuthenticationChallengeContext.Result
false
==null)

true Invoke authorization filters

HttpApplication.BeginProcessRequest
Invoke authorization filters (IAuthorizationFilter)
(IAuthorizationFilter)
Start
HttpApplication.BeginProcessRequest
MvcHandler executes If(AuthorizationContext.Result==null) false
the controller action Controller.OnAuthorization
HttpApplication.EndProcessRequest
true

Invoke authentication challenges


HTTP Response Perform model binding on parameters If(AuthorizationContext.Result==null) false
(IAuthenticationFilter)
true

<IAuthorizationFilter1>.OnAuthorization

Invoke action with action filters


Color Legend (IActionFilter) If(AuthorizationContext.Result==null) false
HttpApplication.EndProcessRequest

true
Controller.EndExecute
HttpApplication <IAuthorizationFilter2>.OnAuthorization

Routing Invoke authentication challenges Controller.EndExecuteCore


(IAuthenticationFilter) If(AuthorizationContext.Result==null) false
Controller
true

Authentication Filters Execute result


...
Execute result with result filters
Authorization Filters (IResultFilter) End
Authentication Challenges
Action Execution
Controller.Dispose Use IAuthorizationFilters to authorize a user action toward the
Result Execution intended resources. Authorization filters execute in order until
one of the filters returns a non-null result.

Invoke authentication challenges


(IAuthenticationFilter)
HttpApplication.BeginProcessRequest

Execute action method with action filters (IActionFilter) Execute results with result filters (IResultFilter)
Start
Controller implements IResultFilter (by inheriting
Controller implements IActionFilter (by inheriting Controller.OnResultExecuting ActionFilterAttribute). OnResultExecuting methods
Controller.OnAuthenticationChallenge
Controller.OnActionExecuting ActionFilterAttribute). OnActionExecuting methods <ResultFilter1>.OnResultExecuting are executed in order before the result is executed.
<ActionFilter1>.OnActionExecuting are executed in order before the action method <ResultFilter2>.OnResultExecuting
itself. ... <IAuthenticationFilter1>.OnAuthenticationChallenge
<ActionFilter2>.OnActionExecuting
...
Asynchronous action methods (modified with the After authentication challenges are executed as a <IAuthenticationFilter2>.OnAuthenticationChallenge
Invoke action results
async keyword and returns Task<T>) are executed in (ActionResult.ExecuteResult) result of failed authentication or failed authorization,
<Named>Controller.<ActionAsync>
HttpApplication.BeginProcessRequest the result is executed immediately without any result ...
View, PartialView Other ActionResult
filters.
Controller.EndExecute Called in HttpApplication.EndProcessRequest to end types
HttpApplication.EndProcessRequest

the controller execution. For asynchronous action End


Find view from
methods, this portion of the pipeline may be invoked
Controller.EndExecuteCore view engines Write to HTTP
in a different worker thread.
(RazorView, etc.) response
Synchronous action methods (not modified with Render view
<Named>Controller.<Action> Authentication challenges are invoked whenever authentication
the async keyword) are executed in
or authorization filters return a result to indicate failure. They
HttpApplication.EndProcessRequest
... ... are also invoked after the action method executes in case the
<ActionFilter2>.OnActionExecuted <ResultFilter2>.OnResultExecuted authentication scheme requires it, such as for server
<ActionFilter1>.OnActionExecuted OnActionExecuted methods are executed in authentication to the client. Use OnAuthenticationChallenge to
<ResultFilter1>.OnResultExecuted OnResultExecuted methods are executed in reverse
reverse order after the action method itself. create the ActionResult you intend to send to the client when
Controller.OnActionExecuted Controller.OnResultExecuted order after the result is executed authentication challenges occur. When authentication
challenges are invoked, all registered IAuthenticationFilter
contribute to the result.

You might also like