Handling Application-Level Events

Files with a .asax extension are not served by Web browsers. However, each Web application is created with a Global.asax file. This file is used to warehouse application and session events. For example, each time a request is made to your Web application, the Application_BeginRequest event is fired . You can use this event as an opportunity to perform some preliminary tasks before the rest of the application code runs.

Listing 15.16 provides a listing for the Global.asax file, demonstrating how the color scheme for the Web site can be placed in the HttpContext object. The HttpContext object, instantiated as Context , is unique to each HTTP request. Here I am placing an instance of ColorInfo into the Context collection. The ColorInfo class could be defined to read the color scheme from a database, which means that if you change the database, you change the colors of the Web site.

Listing 15.16 Adding the ColorInfo Class to HttpContext When a Request Is Made
 1:  Imports System.Web 2:  Imports System.Web.SessionState 3: 4:  Public Class Global 5:      Inherits System.Web.HttpApplication 6: 7:  [ Component Designer generated code ] 8: 9:      Sub Application_Start(ByVal sender As Object, _ 10:       ByVal e As EventArgs) 11:         ' Fires when the application is started 12:     End Sub 13: 14:     Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) 15: 16:     End Sub 17: 18:     Sub Application_BeginRequest(ByVal sender As Object, _ 19:       ByVal e As EventArgs) 20:       Context.Items("ColorInfo") = New ColorInfo(LinkID) 21:     End Sub 22: 23:     Private ReadOnly Property LinkID() 24:     Get 25:       If (Request.QueryString("LinkID") Is Nothing = False) Then 26:         Return Convert.ToInt64(Request.QueryString("LinkID")) 27:       Else 28:         Return 1 29:       End If 30:     End Get 31:     End Property 32: 33:     Sub Application_AuthenticateRequest(ByVal sender As Object, _ 34:       ByVal e As EventArgs) 35:         ' Fires upon attempting to authenticate the user 36:     End Sub 37: 38:     Sub Application_Error(ByVal sender As Object, _ 39:       ByVal e As EventArgs) 40:       ' Fires when an error occurs 41:     End Sub 42: 43:     Sub Session_End(ByVal sender As Object, _ 44:       ByVal e As EventArgs) 45:       ' Fires when the session ends 46:     End Sub 47: 48:     Sub Application_End(ByVal sender As Object, _ 49:       ByVal e As EventArgs) 50:       ' Fires when the application ends 51:     End Sub 52: 53: End Class 

As the Web application evolves, I have found a need to read the color scheme information. By reading it in Global.asax 's Application_BeginRequest event handler (lines 18 through 21) and stuffing it into the Context property, I have to read the color information from the database only once. Subsequent references to ColorInfo will be in memory, read from the Context property.

If you want to know the order or timing of the Global events, you can look up this information in the help documentation or use a System.Diagnostics.Debug.WriteLine statement in each event handler. The order of events is an easy detail to forget.

Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel

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