Understanding MVC Request Life Cycle – Part 2.2

Understanding MVC Request Life Cycle

Application Request Life Cycle Events

Hello Readers, in this post we are going to discuss about Application Request life cycle events.

Links to previous posts

  1. Understanding Model View Controller (MVC)
  2. Understanding MVC Request Life Cycle – Part 2.1

Please note that we are going to discuss Application Request Life Cycle Events here and not the MVC Life Cycle. We will discuss the MVC Life cycle in detail in future posts.

When a request is made to IIS, it is queued in the application pool of the application. An application pool is a group of one or more URLs that are served by a worker process. The worker process (w3wp.exe) is responsible to forward the request to the application.

The request is processed by the HttpApplication pipeline and events are fired in the following order:

  1. BeginRequest – Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request. The BeginRequest event signals the creation of any given new request. This event is always raised and is always the first event to occur during the processing of a request.
  2. AuthenticateRequest – The AuthenticateRequest event signals that the configured authentication mechanism has authenticated the current request. Subscribing to the AuthenticateRequest event ensures that the request will be authenticated before processing the attached module or event handle.
  3. PostAuthenticateRequest – The PostAuthenticateRequest event is raised after the AuthenticateRequest event has occurred. All the information available is accessible in the HttpContext’s User property.
  4. AuthorizeRequest – The AuthorizeRequest event signals that ASP.NET has authorized the current request. You can subscribe to the AuthorizeRequest event to perform custom authorization.
  5. PostAuthorizeRequest – Occurs when the user for the current request has been authorized.
  6. ResolveRequestCache – Occurs when ASP.NET finishes an authorization event to let the caching modules serve requests from the cache, bypassing execution of the event handler and calling any EndRequest handlers.
  7. PostResolveRequestCache – Reaching this event means the request can’t be served from the cache, and thus a HTTP handler is created here. A Page class gets created if an aspx page is requested.
  8. MapRequestHandler – The MapRequestHandler event is used by the ASP.NET infrastructure to determine the request handler for the current request based on the file-name extension of the requested resource.
  9. PostMapRequestHandler – Occurs when ASP.NET has mapped the current request to the appropriate HTTP handler
  10. AcquireRequestState – Occurs when ASP.NET acquires the current state (for example, session state) that is associated with the current request. A valid session ID must exist.
  11. PostAcquireRequestState – Occurs when the state information (for example, session state or application state) that is associated with the current request has been obtained.
  12. PreRequestHandlerExecute – Occurs just before ASP.NET starts executing an event handler.
  13. ExecuteRequestHandler – Occurs when handler generates output. This is the only event not exposed by the HTTPApplication class.
  14. PostRequestHandlerExecute – Occurs when the ASP.NET event handler has finished generating the output
  15. ReleaseRequestState – Occurs after ASP.NET finishes executing all request event handlers. This event signal ASP.NET state modules to save the current request state.
  16. PostReleaseRequestState – Occurs when ASP.NET has completed executing all request event handlers and the request state data has been persisted.
  17. UpdateRequestCache – Occurs when ASP.NET finishes executing an event handler in order to let caching modules store responses that will be reused to serve identical requests from the cache.
  18. PostUpdateRequestCache – When the PostUpdateRequestCache is raised, ASP.NET has completed processing code and the content of the cache is finalized.
  19. LogRequest – Occurs just before ASP.NET performs any logging for the current request. The LogRequest event is raised even if an error occurs. You can provide an event handler for the LogRequest event to provide custom logging for the request.
  20. PostLogRequest – Occurs when request has been logged
  21. EndRequest – Occurs as the last event in the HTTP pipeline chain of execution when ASP.NET responds to a request. In this event, you can compress or encrypt the response.
  22. PreSendRequestHeaders – Fired after EndRequest if buffering is turned on (by default). Occurs just before ASP.NET sends HTTP headers to the client.
  23. PreSendRequestContent – Occurs just before ASP.NET sends content to the client.

Let’s see a practical example by hooking into above events into a WebApp.

I have chosen an empty MVC Project as below

Request Life Cycle Application

Create an empty controller named “Home” (You can name whatever but it’s default controller name so I would like to keep its name as Home). For those who don’t know how to create controller.

  1. Right Click on the controller folder
    Add ControllerA window will appear to select the type of controller.
  2. Select the MVC 5 Empty controller from the list of options and then click AddSelect MVC 5 Empty Controller
    After adding a dialog box will appear asking for name of the controller. Just name it “HomeController” for now.

When you add a controller you will see that a default Action method is created for you. We don’t require it for our demo purpose so replace the default action method created by the following method

Now go to Global.asax file and let’s write down our Application Request Life Cycle Events. After adding the events my Global.asax looks like below

Notice that all events we have created are in following format Application_ NameOfEvent

Put the debugger on each event and also put a debugger in the method we created in controller. Now let’s run the application. Hit F5 on your Visual Studio or Hit Run button

Run the Application

You will see that our application will hit the breakpoint for first Request Life Cycle Event – BeginRequest event. Now Press (CTRL+ALT+V+A). This will open the Autos Window

Autos Window
OR alternatively you can open the watch window to see when Handler comes into the Pipeline

Watch Window

Open the watch Window and type like below

Watch Window

So as we are currently in our Begin Request as per LifeCycle our Handler is Null as expected. Now let’s continue and hit F5 again. Now we will move to MapRequestHandler event and you will see in WatchWindow that a Handler is assigned the value.

Map Handler

Next click again F5 and you will be moved to PostMapRequestHandler. In this we can change the current HTTPHandler because we already have a mapped handler.

Now click again F5 and you will be moved to AcquireRequestState() and PreRequestHandlerExecute() events in a sequence. PreRequestHandlerExecute the last method before which MVCHandler executes.

Now hit F5 again and you will be taken to your HomeController method we created above.

MVCHandlerControllerRequest

If we hit F5 again we’ll land to Application events(PostRequestHandlerExecute() and EndRequest()) that are to be processed after handler has been executed.

This sums up the Application Request Life Cycle Events

Important point to note here is that Application Request Life Cycle events are common to WebForms as well as MVC. We can introduce the same events in a WebForms Application also.

Code can be downloaded from Github

Thanks for reading.

Comments are welcome

Be the first to comment

Leave a Reply

Your email address will not be published.


*