Vous êtes sur la page 1sur 2

ASP.

NET MVC 5 APPLICATION LIFECYCLE HIGH-LEVEL VIEW


HttpApplication Processing Pipeline HTTP Request

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

Action Method Invocation

Action Invocation (with Filters)

Result Execution (View, etc.)

ASP.NET MVC 5 APPLICATION LIFECYCLE DETAIL VIEW


HttpApplication Processing Pipeline HTTP Request
Application_Start (in Global.asax.cs) HttpApplication.Init
Tasks to perform: Common tasks such as register routes, bundles, and filters Advance tasks such as set custom controller factory

Override to subscribe to HttpApplication events

IHttpModule.Init

HttpApplication Processing Pipeline

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.

Invoke authentication filters (IAuthenticationFilter)


Start

Controller.OnAuthentication

If(AuthenticationContext.Result==null) true

false

<IAuthenticationFilter1>.OnAuthentication

IControllerFactory.CreateController <Named>Controller.ctor HttpApplication.PostResolveRequestCache Controller.BeginExecute


UrlRoutingModule.PostResolveRequestCache

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)

Controller.CreateTempDataProvider Controller.CreateActionInvoker Invoke authentication filters (IAuthenticationFilter)


If(AuthenticationChallengeContext.Result ==null) true

Override to use a custom TempData provider. SessionStateTempDataProvider is used by default.


Override to use a custom action invoker. AsyncControllerActionInvoker is used by default.

... End

Store the encapsulated route data (at HttpApplication.Context.Request .RequestContext)

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

Invoke authorization filters (IAuthorizationFilter)

Invoke authorization filters (IAuthorizationFilter)


Start
false

HttpApplication.BeginProcessRequest

HttpApplication.BeginProcessRequest
HttpApplication.EndProcessRequest

MvcHandler executes the controller action

If(AuthorizationContext.Result==null) true

Controller.OnAuthorization

HTTP Response

Perform model binding on parameters

Invoke authentication challenges (IAuthenticationFilter)

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.

Execute action method with action filters (IActionFilter)


Controller implements IActionFilter (by inheriting ActionFilterAttribute). OnActionExecuting methods are executed in order before the action method itself. Asynchronous action methods (modified with the async keyword and returns Task<T>) are executed in HttpApplication.BeginProcessRequest Called in HttpApplication.EndProcessRequest to end the controller execution. For asynchronous action methods, this portion of the pipeline may be invoked in a different worker thread. Synchronous action methods (not modified with the async keyword) are executed in HttpApplication.EndProcessRequest

Execute results with result filters (IResultFilter)


Controller.OnResultExecuting <ResultFilter1>.OnResultExecuting <ResultFilter2>.OnResultExecuting ... Invoke action results (ActionResult.ExecuteResult)
View, PartialView Other ActionResult types

Invoke authentication challenges (IAuthenticationFilter)


Start

HttpApplication.BeginProcessRequest HttpApplication.EndProcessRequest

Controller.OnActionExecuting <ActionFilter1>.OnActionExecuting <ActionFilter2>.OnActionExecuting ... <Named>Controller.<ActionAsync> Controller.EndExecute

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.

<IAuthenticationFilter2>.OnAuthenticationChallenge ... End

Controller.EndExecuteCore
<Named>Controller.<Action> ... <ActionFilter2>.OnActionExecuted <ActionFilter1>.OnActionExecuted Controller.OnActionExecuted

Find view from view engines (RazorView, etc.) Render view

Write to HTTP response


Authentication challenges are invoked whenever authentication or authorization filters return a result to indicate failure. They are also invoked after the action method executes in case the authentication scheme requires it, such as for server authentication to the client. Use OnAuthenticationChallenge to create the ActionResult you intend to send to the client when authentication challenges occur. When authentication challenges are invoked, all registered IAuthenticationFilter contribute to the result.

OnActionExecuted methods are executed in reverse order after the action method itself.

... <ResultFilter2>.OnResultExecuted <ResultFilter1>.OnResultExecuted Controller.OnResultExecuted

OnResultExecuted methods are executed in reverse order after the result is executed

Vous aimerez peut-être aussi