Export (0) Print
Expand All

HttpApplication Class

Defines the methods, properties, and events that are common to all application objects within an ASP.NET application. This class is the base class for applications that are defined by the user in the Global.asax file.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

'Declaration
Public Class HttpApplication
	Implements IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
'Usage
Dim instance As HttpApplication

public class HttpApplication implements IHttpAsyncHandler, IHttpHandler, 
	IComponent, IDisposable
public class HttpApplication implements IHttpAsyncHandler, IHttpHandler, 
	IComponent, IDisposable

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:

  1. BeginRequest

  2. AuthenticateRequest

  3. PostAuthenticateRequest

  4. AuthorizeRequest

  5. PostAuthorizeRequest

  6. ResolveRequestCache

  7. PostResolveRequestCache

    After the PostResolveRequestCache event and before the PostMapRequestHandler event, an event handler (a page corresponding to the request URL) is created.

  8. PostMapRequestHandler

  9. AcquireRequestState

  10. PostAcquireRequestState

  11. PreRequestHandlerExecute

    The event handler is executed.

  12. PostRequestHandlerExecute

  13. ReleaseRequestState

  14. PostReleaseRequestState

    After the PostReleaseRequestState event, response filters, if any, filter the output.

  15. UpdateRequestCache

  16. PostUpdateRequestCache

  17. EndRequest

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

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.VB.CustomHTTPModule"
           name="CustomHttpModule" />
      </httpModules>
  </system.web>
</configuration>

System.Object
  System.Web.HttpApplication

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft