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)

public class HttpApplication : IHttpAsyncHandler, IHttpHandler, IComponent, 
	IDisposable
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.

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>

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