|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
15580
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
The HttpApplication Class
The HttpApplication is another important class that is use to control and manage the global scope of an application. The HttpApplication defines methods, properties, and events that are common to all application objects within an ASP.NET application. Only once instance of HttpApplication is created by the framework and can be used by applications. You don't create this class instance.
In this section, we'll see the HttpApplication class members briefly and we'll use them in the following sections as needed. The HttpApplication class properties are listed in Table 3.
Table 3. The HttpApplication class properties
| Property |
Description |
| Application |
Get the application state. We've discussed in previous section |
| Context |
Gets the HttpContext for the current instance of the application. |
| Modules |
Get the collection of modules of an application in the form of an HttpModuleCollection, which represents a collection of IhttpModule objects. |
| Request |
Gets the request object |
| Response |
Gets the response object |
| Server |
Gets the server object |
| Session |
Gets the session object |
| User |
Gets the user object |
Normally the HttpApplication events are listed in Global.asax file, which we'll discuss in the following section.
Using the Global.asax
Global.asax file is one more we'll discuss in this chapter. Whenever you create a new project, VS.NET creates the Global.asax file. You can find this file in the root of an application. Actually its an optional file and contains application level events raised by ASP.NET or by HTTP modules.
The default events available in this file are listed in Table 4.
Table 4. The Global.asax events
| Event |
Description |
| Application_Start |
Occurs when an application starts. This event occurs only one time in an application’s life cycle. It occurs again when you restart the application. It’s the best place to count number of visitors. |
| Session_Start |
Occurs whenever a new session starts. This is the best place to count user sessions for an application. |
| Application_BeginRequest |
Occurs every time when server makes a request. This event may occur multiple times in an applications life cycle. |
| Application_AuthenticateRequest |
Occurs before a user is being authenticated. |
| Application_Error |
Occurs when an unhandled error or exception occurs in an application. |
| Session_End |
Occurs when a session ends. |
| Application_End |
Occurs when an application closes. |
| User |
Gets the user object |
The default Global.asax of an application added by VS.NET is listed in Listing 1.
Listing 1. The Global.asax file contents
Imports System.Web Imports System.Web.SessionState Public Class Global Inherits System.Web.HttpApplication #Region " Component Designer Generated Code " #End Region Sub Application_Start(ByVal sender As Object,ByVal e As EventArgs) ' Fires when the application is started End Sub Sub Session_Start(ByVal sender As Object,ByVal e As EventArgs) ' Fires when the session is started End Sub Sub Application_BeginRequest(ByVal sender As Object,ByVal e As EventArgs) ' Fires at the beginning of each request End Sub Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs) ' Fires upon attempting to authenticate the use End Sub Sub Application_Error(ByVal sender As Object,ByVal e As EventArgs) ' Fires when an error occurs End Sub Sub Session_End(ByVal sender As Object,ByVal e As EventArgs) ' Fires when the session ends End Sub Sub Application_End(ByVal sender As Object,ByVal e As EventArgs) ' Fires when the application ends End Sub End Class
Using Context in an Application
By default there is no reference of Response, Request, and Page in Global.asax. The Context object is used to get the reference of these objects in the Global.asax file.
The code listed in Listing 2 shows how to calculate users and user sessions in an application. As you can see from this code, we've used Context to get a Response object. If you don't use Context, you'll get compiler errors.
Listing 2. Using Application_Start and Session_Start events
Sub Application_Start(ByVal sender As Object,ByVal e As EventArgs) ' Fires when the application is started Application("Title") = "Job Board" Application.Lock() Application("Users") += 1 Application.UnLock() Context.Response.Write(Application("Users")) End Sub Sub Session_Start(ByVal sender As Object,ByVal e As EventArgs) ' Fires when the session is started Application.Lock() Application("SessionCounter") += 1 Application.UnLock() Context.Response.Write(Application("SessionCounter")) End Sub
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|