Assembly: System.Web (in system.web.dll)
Public Class HttpApplication Implements IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
Dim instance As HttpApplication
public class HttpApplication : IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
public ref class HttpApplication : IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
public class HttpApplication implements IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
public class HttpApplication implements IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
Not applicable.
Instances of the HttpApplication class are created in the ASP.NET infrastructure, not by the user directly. One instance of the HttpApplication class is used to process many requests in its lifetime; however, it can process only one request at a time. Thus, member variables can be used to store per-request data.
An application executes events that are handled by modules or user code that is defined in the Global.asax file in the following sequence:
-
After the PostResolveRequestCache event and before the PostMapRequestHandler event, an event handler (a page corresponding to the request URL) is created.
-
PostMapRequestHandler
-
The event handler is executed.
-
After the PostReleaseRequestState event, response filters, if any, filter the output.
The following two examples demonstrate how to use HttpApplication class and its events. The code example demonstrates how to create a custom HTTP module and connect an event to it. The second demonstrates how to modify the Web.config file.
The following code 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 within 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 code example shows the appropriate configuration setting within the httpModules section of the Web.config file.
<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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
System.Web.HttpApplication
Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1..NET Framework
Supported in: 3.0, 2.0, 1.1, 1.0PreSendRequestContent - http://msdn.microsoft.com/library/system.web.httpapplication.presendrequestcontent.aspx
PreSendRequestHeaders - http://msdn.microsoft.com/library/system.web.httpapplication.presendrequestheaders.aspx