Global.asax files, sometimes called ASP. NET application files, provide a way to respond to application-level or module-level events in a central location. You can use this file for application security and some other tasks. Let's take a look at how to use this file in application development work.
Overview
The Global.asax is located in the application root directory. Although Visual Studio. NET automatically inserts this file into all ASP., it is actually an optional file. Deleting it doesn't make a problem--of course, when you're not using it. The. asax file extension indicates that it is an application file instead of an ASP. Net file that uses ASPX.
The Global.asax file is configured as any direct HTTP request (via URL) is automatically rejected, so the user cannot download or view its contents. The ASP. NET page framework is able to automatically identify any changes to the Global.asax file. After the Global.asax is changed, the ASP. NET page framework restarts the application, including closing all browser sessions, removing all state information, and restarting the application domain.
Programming
The Global.asax file inherits from the HttpApplication class, which maintains a pool of HttpApplication objects and assigns the objects in the object pool to the application when needed. The Global.asax file contains the following events:
· Application_init: The event is triggered when an application is instantiated or first called. For all HttpApplication object instances, it will be called.
· Application_disposed: Triggered before the application is destroyed. This is the ideal location to clear the resources used previously.
· Application_Error: The event is triggered when an unhandled exception is encountered in the application.
· Application_Start: The event is triggered when the first instance of the HttpApplication class is created. It allows you to create objects that can be accessed by all HttpApplication instances.
· Application_End: The event is triggered when the last instance of the HttpApplication class is destroyed. It is only triggered once during the lifetime of an application.
· Application_BeginRequest: Triggered when an application request is received. For a request, it is the first event to be triggered, and the request is typically a page request (URL) entered by the user.
· Application_EndRequest: The last event requested for the application.
· Application_prerequesthandlerexecute: The event is triggered before an ASP. NET page framework starts executing an event handler, such as a page or Web service.
· Application_postrequesthandlerexecute: The event is triggered when an event handler is executed at the end of the ASP.
· Applcation_presendrequestheaders: The event is triggered when the ASP. NET page framework sends an HTTP header to the requesting client (browser).
· Application_presendcontent: The event is triggered when the ASP. NET page framework sends content to the requesting client (browser).
· Application_acquirerequeststate: The event is triggered when the ASP. NET page framework obtains the current state (Session state) associated with the current request.
· Application_releaserequeststate: The event is triggered when all event handlers are executed in the ASP. This causes all state modules to save their current state data.
· Application_resolverequestcache: The event is triggered when an authorization request is completed on the ASP. It allows the cache module to service the request from the cache, bypassing the execution of the event handler.
· Application_updaterequestcache: When the ASP. NET page framework completes the execution of an event handler, the event is triggered so that the cache module stores the response data for use in response to subsequent requests.
· Application_AuthenticateRequest: The event is triggered when the security module establishes a valid identity for the current user. At this point, the user's credentials will be verified.
· Application_authorizerequest: The event is triggered when the security module confirms that a user has access to the resource.
· Session_Start: The event is triggered when a new user accesses the application Web site.
· Session_End: The event is triggered when a user's session times out, ends, or they leave the application Web site.
This list of events may seem intimidating, but these events can be very useful in different environments.
One of the key issues in using these events is knowing the order in which they are triggered. The Application_init and Application_Start events are triggered the first time the application is started. Similarly, the application_disposed and Application_End events are triggered once when the application terminates. In addition, session-based events (Session_Start and session_end) are only used when the user enters and leaves the site. The rest of the events handle application requests, and the order in which these events are triggered is:
· Application_BeginRequest
· Application_AuthenticateRequest
· Application_authorizerequest
· Application_resolverequestcache
· Application_acquirerequeststate
· Application_prerequesthandlerexecute
· Application_presendrequestheaders
· Application_presendrequestcontent
· << Code Execution >>
· Application_postrequesthandlerexecute
· Application_releaserequeststate
· Application_updaterequestcache
· Application_EndRequest
These events are often used in terms of security. The following example of C # demonstrates the different Global.asax events, which use the Application_authenticate event to complete forms-based authentication via a cookie. Additionally, the Application_Start event populates an application variable, and Session_Start populates a session variable. The Application_Error event displays a simple message describing the error that occurred.
protected void Application_Start (Object sender, EventArgs e) {
application["Title"] = "builder.com Sample";
}
protected void Session_Start (Object sender, EventArgs e) {
session["Startvalue"] = 0;
}
protected void Application_AuthenticateRequest (Object sender, EventArgs e) {
Extract The Forms authentication cookie
string cookiename = Formsauthentication.formscookiename;
HttpCookie Authcookie = Context.request.cookies[cookiename];
if (null = = Authcookie) {
There is no authentication cookie.
Return
}
FormsAuthenticationTicket AuthTicket = null;
try {
AuthTicket = Formsauthentication.decrypt (Authcookie.value);
} catch (Exception ex) {
Log exception Details (omitted for simplicity)
Return
}
if (null = = AuthTicket) {
Cookie failed to decrypt.
Return
}
When the ticket is created, the UserData property is assigned
A pipe delimited string of role names.
STRING[2] Roles
Roles[0] = "one"
ROLES[1] = "both"
Create an Identity object
FormsIdentity id = new FormsIdentity (AuthTicket);
This principal would flow throughout the request.
GenericPrincipal principal = new GenericPrincipal (ID, roles);
Attach the new principal object to the current HttpContext object
Context.User = Principal;
}
protected voidApplication_Error (Object sender, EventArgs e) {
Response.Write ("Error encountered.");
}
This example simply uses some of the events in the Global.asax file, and it is important to realize that these events are related to the entire application. In this way, all the methods that are placed in it are provided through the application's code, which is why it is named Global.
Resources
The Global.asax file is the central point of an ASP. It provides countless events to handle different application-level tasks, such as user authentication, application startup, and processing of user sessions. You should familiarize yourself with this optional file so that you can build a robust ASP.
Attached: *.ascx *.asax *.aspx.resx *.asax.resx is what file
SLN: A solution file that provides Solution Explorer with the information needed to display a graphical interface for managing files.
. csproj: Project files, creating information for references, data connections, folders, and files required by your application.
The. Aspx:web form page consists of two parts: visual elements (HTML, server controls, and static text) and the programming logic of the page. Visual Studio Stores the two components separately in a separate file. Visual elements are created in the. aspx file.
The programming logic for the. Aspx.cs:Web form page is in a separate class file called the code-behind class file (. aspx.cs).
. CS: Class module code file. The code for the business logic processing layer.
A. asax:Global.asax file (also known as an ASP. NET application file) is an optional file that contains code that responds to application-level events raised by the ASP. NET or HTTP module.
. config:Web.config files provide configuration information to the directory in which they reside and to all subdirectories.
. Aspx.resx/.resx: Resource file, a resource is any non-executable data that is logically deployed by an application. By storing data in a resource file, you can change the data without recompiling the entire application.
. A kind of xsd:xml schema. From DTD,XDR development to XSD
The. PDB:PDB (program database) file maintains debugging and project state information, allowing you to incrementally link the Debug configuration of your program.
The. Suo: Solution user option, which records all the options that will be associated with the solution, so that each time it is opened, it contains the customizations that you made.
. asmx:asmx files contain WebService processing directives and are used as addressable entry points for XML Web Services
The. VSDISCO (Project Discovery) file is an XML-based file that contains a link (URL) to a resource that provides discovery information for a WEB service.
. HTC: An HTML file that contains a series of HTC-specific elements for scripting and defining components. HTC provides a mechanism for implement components in scripts
. ascx is a user control code file
. aspx webform HTML script files
. CS is a C # class file)
. vb is a VB class file)
. Aspx.cs and your WebForm-related background C # code files are actually the same as. cs
. Aspx.vb and your WebForm-related background VB code files, in fact, with. vb is the same
Web. config configuration file
. XML XML file
. CSS style sheet files
ASP. What is the Global.asax file in net?