What is the purpose of global.aspx file in .net?

Global.asax is a file used to declare application-level events and objects. Global.asax is the ASP.NET extension of the ASP Global.asa file. Code to handle application events (such as the start and end of an application) reside in Global.asax. Such event code cannot reside in the ASP.NET page or web service code itself, since during the start or end of the application, its code has not yet been loaded (or unloaded). Global.asax is also used to declare data that is available across different application requests or across different browser sessions. This process is known as application and session state management.

The Global.asax file must reside in the IIS virtual root. Remember that a virtual root can be thought of as the container of a web application. Events and state specified in the global file are then applied to all resources housed within the web application. If, for example, Global.asax defines a state application variable, all .aspx files within the virtual root will be able to access the variable.

Like an ASP.NET page, the Global.asax file is compiled upon the arrival of the first request for any resource in the application. The similarity continues when changes are made to the Global.asax file: ASP.NET automatically notices the changes, recompiles the file, and directs all new requests to the newest compila


...........................................................................................................................................
The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HttpModules. The Global.asax file resides in the root directory of an ASP.NET-based application. At run time, Global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class. The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code written within it.
The ASP.NET Global.asax file can coexist with the ASP Global.asax file. You can create a Global.asax file either in a WYSIWYG designer, in Notepad, or as a compiled class that you deploy in your application's \Bin directory as an assembly. However, in the latter case, you still need a Global.asax file that refers to the assembly.
The Global.asax file is optional. If you do not define the file, the ASP.NET page framework assumes that you have not defined any application or session event handlers.
When you save changes to an active Global.asax file, the ASP.NET page framework detects that the file has been changed. It completes all current requests for the application, sends the Application_OnEnd event to any listeners, and restarts the application domain. In effect, this reboots the application, closing all browser sessions and flushing all state information. When the next incoming request from a browser arrives, the ASP.NET page framework reparses and recompiles the Global.asax file and raises the Application_OnStart event.

Using Modules with the Global.asax File

.NET Framework 1.1
ASP.NET provides several modules that participate in each request and expose events you can handle in Global.asax. You can customize and extend these modules as you like, or develop completely new custom modules to process information for and about HTTP requests made to your ASP.NET-based application. For example, you could create an output cache module that implements output-caching behaviors for your entire application.
All modules, whether custom or provided by the .NET Framework, must implement the IHttpModule interface. As long as these modules are registered with your application, you can easily interact with the HTTP requests coming in to your application.

Handling HttpModule Events

You can use the Global.asax file to handle any event exposed by the modules in the request. For example, you might create a custom authentication module for your ASP.NET Web application in which you might expose an OnAuthenticateRequest event. The code that you write to handle the events exposed by an HttpModule must conform to the following naming pattern:
FriendlyModuleName_EventName(AppropriateEventArgumentSignature)
For example, if you want to include event-handling code for the beginning and end of a session, as well as for an OnAuthenticateRequest event, it could look like the following.



<Script language="VB" runat="server">
     Sub Session_OnStart()
         'Session start-up code goes here.
     End Sub
     Sub Session_OnEnd()
         'Session clean-up code goes here.
     End Sub
     Sub Application_OnAuthenticateRequest(Source As Object, Details as EventArgs)
         'Authentication code goes here.
     End Sub
</script>
[C#]
<Script language="C#" runat="server">
     void Session_OnStart() {
         // Session start-up code goes here.
     }
     void Session_OnEnd() {
         // Session clean-up code goes here.
     }
     void Application_OnAuthenticateRequest(Object Source, EventArgs Details) {
         // Authentication code goes here.
     }
</script>

1 comments:

lingmaaki said...

More about......global.asax

Ling

Post a Comment