This is the “HttpHanlder and HttpModule in real scenario” article series. The purpose of this article is to understand the real-time uses of HttpHandler and HttpModule in ASP.NET application development. In our previous article, we saw a few practical examples of HttpHandler and we started to talk about HttpModule. You can read them here.
- HttpHandler and HttpModule in Real Scenario: Getting Started with HttpHandler
- HttpHandler and HttpModule in Real Scenario: Few examples of HttpHandler-1
- Working with Image in HttpHandler
- Working With Header in HttpHandler
- HttpModule in real scenario: Implement simple HttpModule
- HttpModule in Real Scenario: Multiple HttpModule and Communication
In our previous HttpModule article we saw that we can bind various events associated with a HTTP request and response context.
In this article, we will talk about various events and their execution sequence in an ASP.NET application. Here is the list of events depending on execution sequence.
BeginRequest
Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page or some variable initialization).
AuthenticateRequest
If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password or checking of header information in HTTP request).
AuthorizeRequest
This event is used internally to implement authorization mechanisms (for example, to store your Access Control Lists (ACLs) in a database rather than in the file system).
ResolveRequestCache
This event determines if a page can be served from the Output Cache. If you want to write your own caching module (for example, build a file-based cache rather than a memory cache) then synchronize this event to determine whether to serve the page from the cache or a fresh page will be generated.
AcquireRequestState
Session state is retrieved from the state store. If you want to build your own state management module then synchronize this event to grab the Session state from your state store.
PreRequestHandlerExecute
This event occurs before the HTTP handler is executed.
PostRequestHandlerExecute
This event occurs after the HTTP handler is executed.
ReleaseRequestState
Session state is stored back in the state store. If you are building a custom session state module then you must store your state back in your state store.
UpdateRequestCache
This event writes output back to the Output Cache.
EndRequest
Request has been completed.
It does rely impossible to understand those entire events by reading once. To understand them with a practical example we need to implement them one by one in an application. In this example we will try to understand a few of them.
Check whether or not it is authenticated
We can perform the authentication process within an AuthenticateRequest event. In this example we will check whether the user’s authentication is performed before processing the request.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Xml.Linq;
- using Newtonsoft.Json;
- namespace WebApp
- {
- public class MyHttpModule1 : IHttpModule
- {
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- public void Init(HttpApplication context)
- {
- context.BeginRequest += new EventHandler(begin_request);
- context.AuthenticateRequest += new EventHandler(authentication_request);
- }
- public void begin_request(Object sender, EventArgs e)
- {
- }
- public void authentication_request(Object sender, EventArgs e)
- {
- HttpContext Context = ((HttpApplication)sender).Context;
- if (Context.User == null)
- {
- //User is not authenticated user. So drop request here.
- Context.Response.Write("<h2><font color=red>" +
- "Please login before go ahead" +
- "</font></h2><hr>");
- }
- }
- }
- }




Join the conversation! Your thoughts help the community grow.