HttpModules and Global.aspx

   

A couple of things are important to note about the Global.aspx file with regard to HttpModules. First, the Global.aspx file can handle any of the HttpApplication events that are exposed to the HttpModules. Second, events generated in an HttpModule can be handled in the Global.aspx file.

All events in HttpModules are automatically exposed when the module is registered upon the application starting. To handle the event, you must write an appropriate event handler that needs to be named as follows when the handler is in a script block of the Global.aspx file:

 // C#  public FriendlyModuleName_OnEventName(appropriate_event_argument_signature)  {      //Do something here  }  'VB  Public Sub FriendlyModuleName_OnEventName(appropriate_event_argument_signature)      'Do something here  End Sub 

If the event is handled in a class in the Global.aspx's code-behind file, its signature should be as follows:

 // C#  public FriendlyModuleName_EventName(appropriate_event_argument_signature)  {      //Do something here  }  'VB  Public Sub FriendlyModuleName_EventName(appropriate_event_argument_signature)      'Do something here  End Sub 

The friendly module name is the module name used when adding the module in the <HttpModules> section of the web.config or machine.config files.

Although the HttpApplication class is not an HttpModule, its events are handled identically to those of an HttpModule's except that Application should be used as the friendly module name. Here is an example of an event handler for an HttpApplication event defined in a script block of the Global.aspx file:

 // C#  public Application_OnBeginRequest(Object sender, EventArgs e)  {      //Do something here  }  'VB  Public Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)      'Do something here  End Sub 

Handling an event like the OnBeginRequest event in the Global.asax file enables you to initialize application data in a centralized location. If you are handling events in an application-specific manner, it might be easier to handle them in the Global.asax file rather than write an HttpModule to handle the event. The power of using HttpModules is more apparent when the HttpModule is used to handle events in more than one Web application.

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net