The Global.asax file, is an optional file that contains code for responding to application-level events. It resides in the root directory of an ASP.NET-based application. It is configured in a way where any direct URL request for it is automatically rejected; So external users are not able to download or view the code written in it. It is an optional file so if it doesn’t exist in the application then the framework assumes that you have not defines any application or session event handlers. Please go through the below listed some of the basic events of global.asax.
Events | Use of Event |
Application_Init | This event will get fire when the application initializes for the first time. |
Application_Start | This event will get fire when application starts. |
Application_BeginRequest | This event will get fire each time a new request comes in. |
Application_EndRequest | This event will get fire when the request ends. |
Application_AuthenticateRequest | This event indicates that a request is ready to authenticate. |
Application_AuthorizeRequest | After authenticate the user in this event you can assign user with special privileges. |
Application_Error | This event will get fire when an unhandled error occurs within the application. |
Application_End | This event will get fire when the application ends or times out. |
Application_ResolveRequestCache | This will be called when application finishes an authorization event to allow the caching modules serve requests from the output cache. |
Application_AcquireRequestState | The event is called just before session specific information is retrieved for the client and used to populate the Session collection. |
Application_PreRequestHandlerExecute | This event is called before the appropriate HTTP handler executes the request. |
Application_PostRequestHandlerExecute | This event is called after the request is handled. |
Application_ReleaseRequestState | This will be called when session-specific information is about to be serialized from the Session collection. |
Application_UpdateRequestCache | This will be called before information is added to the output cache. |
Session_Start | This event will get fire for the first time when a user’s session is started. |
Session_End | This event will get fire whenever a single user Session ends or times out. |
The best example to use global.asax is the no of users visiting our web sites. You can store the values in application state and retrive it when necessory. To view the no of users accessing your application just create one application variable on application_start event. This the event which will called first when your application first time starts so the variable will be keep unchanged until your application restarts. After creating the “NoOfUser” variable in your application_start use session_start event to increament the variable by 1 so each time when user session starts the variable will be incremented by 1. You can use this variable through out your application.
void Application_Start(object sender, EventArgs e) { Application["NoOfUser"] = 0; } void Session_Start(object sender, EventArgs e) { Application.Lock(); Application["NoOfUser"] = (int)Application["NoOfUser"] + 1; Application.UnLock(); }
create one simple label and assign the application variable to that label using below code.
lblUser.Text = Application["NoOfUser"].ToString();
Hope you enjoy this post. Please give us your valuable comments/feedbacks, so we can deliver more fruitful content.