Export (0) Print
Expand All
10 out of 17 rated this helpful - Rate this topic

HttpApplication Class

Defines the methods, properties, and events that are common to all application objects in 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)
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class HttpApplication : 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 raises events that can be handled by custom modules that implement the IHttpModule interface or by event handler code that is defined in the Global.asax file. Custom modules that implement the IHttpModule interface can be put in the App_Code folder or in a DLL in the Bin folder.

NoteNote:

When running IIS 7.0 in Integrated mode, custom modules in the App_Code folder or Bin folder apply to all requests in the request pipeline. Event handler code in the Global.asax file only applies to requests that are mapped to an ASP.NET handler.

The application events are raised in the following order:

  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 (which is a page that corresponds to the request URL) is created. When a server is running IIS 7.0 in Integrated mode and at least the .NET Framework version 3.0, the MapRequestHandler event is raised. When a server is running IIS 7.0 in Classic mode or an earlier version of IIS, this event cannot be handled.

  8. PostMapRequestHandler

  9. AcquireRequestState

  10. PostAcquireRequestState

  11. PreRequestHandlerExecute

    The event handler is executed.

  12. PostRequestHandlerExecute

  13. ReleaseRequestState

  14. PostReleaseRequestState

    After the PostReleaseRequestState event is raised, any existing response filters will filter the output.

  15. UpdateRequestCache

  16. PostUpdateRequestCache

  17. LogRequest.

    This event is supported in IIS 7.0 Integrated mode and at least the .NET Framework 3.0

  18. PostLogRequest

    This event is supported IIS 7.0 Integrated mode and at least the .NET Framework 3.0

  19. EndRequest

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.

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>

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>
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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.