Create A Globalasax File

You can use the Global.asax file to create special subroutines, called event handlers, which are executed by the Web server when specific events occur. An application can only have one Global.asax file.The purpose of the Global.asax file in ASP.NET is similar to its purpose in ASP 3.0. You should place this file in the root directory of the application. The extension of .asax tells the Web server that it is a Global Application file.

The Application_Start event fires when a user accesses the first .aspx page after you do one of the following things: a) reboot the Web server, b) stop and restart the World Wide Web service, c) update the global.asax file, or d) deploy the application for the first time. Getting the first .aspx page served requires a little extra time because the code in the Application_Start event is being executed. You can use this event to set variables using the Application object, to write application information to a log file, or to do anything else that is required when the application starts.

The Application_End event is fired when the Web server is being shut down. You can use this event to clean up variables created in the Application_Start event, to log any additional Application information, or to execute any other functionality required when shutting down the application.

CREATE A GLOBAL.ASAX FILE

CREATE A GLOBAL.ASAX FILE

□ Start your text editor to create a Global.as, file.

0 Type <SCRIPT LANUAGE="C#" RUNAT="Server"> and press Enter twice.

To create the event handler for when the application starts, click between the <SCRIPT> and </SCRIPT> tags, type void Application_Start() {, and press Enter.

Note: This code will create an Application variable.

□ Start your text editor to create a Global.as, file.

0 Type <SCRIPT LANUAGE="C#" RUNAT="Server"> and press Enter twice.

To create the event handler for when the application starts, click between the <SCRIPT> and </SCRIPT> tags, type void Application_Start() {, and press Enter.

Press Tab to indent, type the code you want to execute when the application starts, and press Enter.

Note: This code will create an Application variable.

Note: Indent the code so that it is easier to read.

^0 Type } to finish the event handler and press Enter.

ASP.NET APPLICATIONS AND STATE MANAGEMENT

Extra

You can specify the language you want to use in the <SCRIPT> tag by setting the language attribute. This could include C# or VB. You will always want to specify the RUNAT attribute to run at the server because that is where the Application and Session objects are located. Here is what the <SCRIPT> tag would look like when using C# as the language: <SCRIPT LANGUAGE="C#" RUNAT="Server">

The Globaliasax file is an optional file. When ASP.NET finds no Global.asax file, the assumption is that there is no need to code for the application and session events.

The Global.asax file is configured so that you cannot request it as a URL. You should set up permissions on the file so that unauthorized users cannot read or update the file, especially if you have any sensitive configuration information in the file.

You do not need to stop the Web server when updating the Global.asax file. When you save changes to the Global.asax file, ASP.NET automatically detects that you have changed the file.ASP.NET then restarts the application.

Untitled - Notepad

E="C#" RUNAT="Server"> rt(){

iteName = "My Lifetime Goals"; >n["applicationSiteName"] = strSiteName, id(){

>n["applicationSiteName"] = "";

Untitled - Notepad

E="C#" RUNAT="Server"> rt(){

iteName = "My Lifetime Goals"; >n["applicationSiteName"] = strSiteName, id(){

>n["applicationSiteName"] = "";

Click File O Save to open the dialog box.

-Q To create the event handler for when the application ends, click between the <SCRIPT> and </SCRIPT> tags, type void Application_End() {, and press Enter.

O Press Tab to indent, type the code you want to execute when the application ends, and press Enter.

_• Type } to finish the event handler and press Enter.

Click File O Save to open the dialog box.

CONTINUED

Was this article helpful?

0 0

Post a comment