From getting client request to sending a response, the Asp.NET MVC lifecycle is bit complex. This lifecycle is totally different from old Asp.NET Webforms. As the page in Webforms has page lifecycle many people compare this with Asp.NET MVC lifecycle which is totally a different thing.
The thing is Asp.NET MVC and Webforms are both implemented using HttpHandlers over Asp.NET and the way they respond to request is different based on their implementation. Usually in Webforms the url points to aspx page which get processed and generate response. However, MVC is bit indirect in this process. In MVC, url gets routed to specific controller and action methods to generate a response.
This article introduces Request Life Cycle and Application Events that takes place as the request travel through MVC framework and Asp.NET platform. It also explains different methods to interact with this events. Let’s get started.
- MVC Request Life Cycle
- Http Application Class
- Global.asax Events
- PreApplicationStart method
- Working with Events Across Frameworks Using HttpModules
- Further Reading
MVC Request Life Cycle
Before we start discussing the life cycle, let’s briefly understand the concept of HttpHandlers and HttpModules.
Handlers are responsible for generating the actual response in MVC. They implement the IHttpHandler class and only one handler will execute per request. On the other hand, HttpModules are created in response to life cycle events. Modules can, for example, be used for populating HttpContext objects. A request can use many modules. These classes derive from IHttpModule.
The thing is these HttpModules and HttpHandlers are the entry points to the ASP.NET MVC framework. Requests to an ASP.NET MVC-based Web application first pass through the UrlRoutingModule object, which is an HttpModule. This module parses the request and performs route selection.
From the selected Route object, the UrlRoutingModule object obtains the IRouteHandler object that is associated with the Route object. Typically, in an MVC application, this will be an instance of MvcRouteHandler.
The IRouteHandler instance creates an IHttpHandler object and passes it the IHttpContext object. By default, the IHttpHandler instance for MVC is the MvcHandler object. The MvcHandler object then selects the controller that will ultimately handle the request.
You can download a PDF document from here that charts the lifecycle of every ASP.NET MVC 5 application, from receiving the HTTP request to sending the HTTP response back to the client.
Http Application Class
MVCApplication class in Global.asax file inherits
HttpApplication Class defines the methods, properties, and events that are common to all application objects in an ASP.NET application. This class is the base class for applications defined by the user in the Global.asax file.
HttpApplication class has number of application and request level events which you can override.
Below is a complete list of events you can override in HttpApplication class.
Application_Init: Fired when an application initializes or is first called. It is invoked for all HttpApplication object instances.
Application_Disposed: Fired just before an application is destroyed. This is the ideal location for cleaning up previously used resources.
Application_Error: Fired when an unhandled exception is encountered within the application.
Application_Start: Fired when the first instance of the HttpApplication class is created. It allows you to create objects that are accessible by all HttpApplication instances.
Application_End: Fired when the last instance of an HttpApplication class is destroyed. It is fired only once during an application’s lifetime.
Application_BeginRequest: Fired when an application request is received. It is the first event fired for a request, which is often a page request (URL) that a user enters.
Application_EndRequest: The last event fired for an application request.
Application_PreRequestHandlerExecute: Fired before the ASP.NET page framework begins executing an event handler like a page or Web service.
Application_PostRequestHandlerExecute: Fired when the ASP.NET page framework has finished executing an event handler.
Applcation_PreSendRequestHeaders: Fired before the ASP.NET page framework sends HTTP headers to a requesting client (browser).
Application_PreSendContent: Fired before the ASP.NET page framework send content to a requesting client (browser).
Application_AcquireRequestState: Fired when the ASP.NET page framework gets the current state (Session state) related to the current request.
Application_ReleaseRequestState: Fired when the ASP.NET page framework completes execution of all event handlers. This results in all state modules to save their current state data.
Application_ResolveRequestCache: Fired when the ASP.NET page framework completes an authorization request. It allows caching modules to serve the request from the cache, thus bypassing handler execution.
Application_UpdateRequestCache: Fired when the ASP.NET page framework completes handler execution to allow caching modules to store responses to be used to handle subsequent requests.
Application_AuthenticateRequest: Fired when the security module has established the current user’s identity as valid. At this point, the user’s credentials have been validated.
Application_AuthorizeRequest: Fired when the security module has verified that a user can access resources.
Session_Start: Fired when a new user visits the application Web site.
Session_End: Fired when a user’s session times out, ends, or they leave the application Web site.
MVC Request lifecycle contains events like Appliication Start and Application End which we can configure in Global.asax file.
We sometimes need to execute some custom code before all this events take place in the first place, like registering a HttpModule. In that case we can use PreApplicationStart method to register this HttpModules.
The PreApplicationStart method is defined at the assembly level using an attribute. We can define the name of a type as well as a method on that type that we want to run before our application starts. Below is the example,
Working with Events Across Frameworks Using HttpModules
As I mentioned earlier both Webforms and MVC frameworks uses HttpModule and HttpHandler for their request and response pipeline. We are interested in HttpModule here. We can set any number of HttpModules in an ASP.NET Http request and response pipeline.
One of the greatest feature of ASP.Net Application Life Cycle that its events can be accessed across frameworks. ASP.NET application whether it’s built on MVC or not. In fact, we can have two applications running side by side using different frameworks and still leverage the same life cycle events.
For example, you could have a webforms application and an MVC application running side by side. You could then build an HttpModule to hook into the same life cycle events for both and perform some common tasks.
Below is a sample code for a HttpModule that registers for HttpApplication events using event handlers.
- ASP.NET MVC Life Cycle by Rasmita Dash - Learn more about Asp.NET MVC Request Life Cycle conceptually from this well-documented article.
- Global.asax and the HttpApplication class by Scott Allen - This article discusses the HttpApplication class and event handlers such as
Session_Start. By reading this article you will get more comfortable in utilizing these events.
- How do ASP.NET Application_ Events Work by Rick Strahl- We learned about ASP.NET Global.asax Application_ Event name handlers, but do you know how it is that these events are actually fired automatically by ASP.NET even when you add additional events by name? In this post, Rick explains how ASP.NET hooks up these methods to HttpModule events.
- HttpModule in Real Scenario: Events in HttpModules by Sourav Kayal - In this article, you will learn more about the execution sequence of various events, discussed before, in a HttpModule.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox