ASP Web Application Quick Start for. NET (3)
File Global. asax
In addition to writing the UI (Use Interface: User Interface), we can also add "Application"-level control logic code and event-triggered programs in the Web application. These codes do not operate and generate the UI, and basically do not respond to individual page requests. They are responsible for handling high-level application events, including Application_Start, Application_End, Session_Start, and Session_End. These control logic codes are stored on the Web
In the Global. asax file under the root directory of the virtual directory where the application is located, ASP. NET automatically parses the file and compiles it into a dynamic. NET Framework class. This class extends the base class of HttpApplication. When any resource or URL in the application namespace is accessed for the first time, it is created.
When the Global. asax file is set, any direct URL request for the Global. asax file will be automatically rejected, thus preventing external users from downloading it and browsing its content.
Events within the scope of Application or Session
You can write a method in the Global. asa file to define an event trigger program for the HTTP Application base class. The method name must match the string "Application _ event name ". For example, see the following code written in three languages: VB, C #, and JScript:
C #
<Script language = "C #" runat = "server">
Void Application_Start (){
// Application startup code goes here
}
</Script>
VB
<Script language = "VB" runat = "server">
Sub Application_Start ()
'Application startup code goes here
End Sub
</Script>
JScript
<Script language = "JScript" runat = "server">
Function Application_Start (): void {
// Application startup code goes here
}
</Script>
If you need to enter another namespace for the event triggering code, you can write an input identifier similar to the following code on the. aspx page:
<% @ Import Namespace = "System. Text" %>
The following example illustrates the lifetime of the Application, Session, and Request:
Application1.aspx
[] Click here to run the routine]
| [] View Source Code]
When the page is opened for the first time, the Onstart event of the application and session will be activated. For the event-triggered programs, see the code written in the following three languages: VB, C #, and JScript:
C #
Void Application_Start (){
Response. Write ("Application is Starting ...");
}
Void Session_Start (){
Response. Write ("Session is Starting ...");
Session. Timeout = 1;
}
VB
Sub Application_Start ()
Response. Write ("Application is Starting ...")
End Sub
Sub Session_Start ()
Response. Write ("Session is Starting ...")
Session. Timeout = 1
End Sub
JScript
Function Application_Start (): void {
Response. Write ("Application is Starting ...");
}
Function Session_Start (): void {
Response. Write ("Session is Starting ...");
Session. Timeout = 1;
}
The BeginRequest and EndRequest events are activated each time a request occurs. For example, when a page is refreshed
And the Page_Load method. Note: When the current session is abandoned (click the "End this session" button), a new session is generated and the Session_OnStart event is triggered again.