The following two examples demonstrate how to use the HttpApplication class and its events. The first example demonstrates how to create a custom HTTP module and connect an event to it. The second example demonstrates how to modify the Web.config file.
The following example demonstrates how to create a custom HTTP module and connect the AcquireRequestState event to the HTTP module. HTTP modules intercept each request to Web application resources, thereby allowing you to filter client requests. Any HTTP module that subscribes to an HttpApplication event must implement the IHttpModule interface.
Imports System
Imports System.Web
Namespace Samples.AspNet.VB
Public Class CustomHTTPModule
Implements IHttpModule
Public Sub New()
' Class constructor.
End Sub
' Classes that inherit IHttpModule
' must implement the Init and Dispose methods.
Public Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init
AddHandler app.AcquireRequestState, AddressOf app_AcquireRequestState
AddHandler app.PostAcquireRequestState, AddressOf app_PostAcquireRequestState
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
' Add code to clean up the
' instance variables of a module.
End Sub
' Define a custom AcquireRequestState event handler.
Public Sub app_AcquireRequestState(ByVal o As Object, ByVal ea As EventArgs)
Dim httpApp As HttpApplication = CType(o, HttpApplication)
Dim ctx As HttpContext = HttpContext.Current
ctx.Response.Write(" Executing AcquireRequestState ")
End Sub
' Define a custom PostAcquireRequestState event handler.
Public Sub app_PostAcquireRequestState(ByVal o As Object, ByVal ea As EventArgs)
Dim httpApp As HttpApplication = CType(o, HttpApplication)
Dim ctx As HttpContext = HttpContext.Current
ctx.Response.Write(" Executing PostAcquireRequestState ")
End Sub
End Class
End Namespace
using System;
using System.Web;
namespace Samples.AspNet.CS
{
public class CustomHTTPModule : IHttpModule
{
public CustomHTTPModule()
{
// Class constructor.
}
// Classes that inherit IHttpModule
// must implement the Init and Dispose methods.
public void Init(HttpApplication app)
{
app.AcquireRequestState += new EventHandler(app_AcquireRequestState);
app.PostAcquireRequestState += new EventHandler(app_PostAcquireRequestState);
}
public void Dispose()
{
// Add code to clean up the
// instance variables of a module.
}
// Define a custom AcquireRequestState event handler.
public void app_AcquireRequestState(object o, EventArgs ea)
{
HttpApplication httpApp = (HttpApplication)o;
HttpContext ctx = HttpContext.Current;
ctx.Response.Write(" Executing AcquireRequestState ");
}
// Define a custom PostAcquireRequestState event handler.
public void app_PostAcquireRequestState(object o, EventArgs ea)
{
HttpApplication httpApp = (HttpApplication)o;
HttpContext ctx = HttpContext.Current;
ctx.Response.Write(" Executing PostAcquireRequestState ");
}
}
}
Before an event in a custom HTTP module can occur, you must modify the configuration settings in the Web.config file to notify ASP.NET about the HTTP module. The following example shows the appropriate configuration setting in the httpModules section of the Web.config file. The following setting applies to IIS 7.0 Classic mode and to earlier versions of IIS.
<configuration>
<system.web>
<httpModules>
<add type="Samples.AspNet.CS.CustomHTTPModule"
name="CustomHttpModule" />
</httpModules>
</system.web>
</configuration>
<configuration>
<system.web>
<httpModules>
<add type="Samples.AspNet.VB.CustomHTTPModule"
name="CustomHttpModule" />
</httpModules>
</system.web>
</configuration>
The following setting applies to IIS 7.0 Integrated mode.
<configuration>
<system.webServer>
<modules>
<add type="Samples.AspNet.CS.CustomHTTPModule"
name="CustomHttpModule" />
</modules>
</system.webServer>
</configuration>
<configuration>
<system.webServer>
<modules>
<add type="Samples.AspNet.VB.CustomHTTPModule"
name="CustomHttpModule" />
<modules>
</system.webServer>
</configuration>