Application start in global.asax

I am developing my application in asp.net 3.5 and sql server 2005, and I want to record the visitor info into my database, like once the visitor enter my website, I'll insert his browser details to the database. [It's not necessary that visitor login my website].

Now I am confused where to put my code, If I put insert function in every page_load then on every page it will execute and I'll not be able to get the exact number of visitor, visited my website. Shall I go with application_start in Global.asax ??

Please help.

You could use session_start in the global.asax, that way it is only happening once per each user's visit.

Application_Start() event in global.asax, ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance. So When first user will open the site it  To add a global.asax file to an application in Visual Studio . choose Website , Add New Item, and. select the Global Application Class file type. Then, click OK. The global.asax file looks similar to a normal .aspx file, except that it can't contain any HTML or ASP.NET tags. Instead, it contains event handlers that respond to application events.

I'd probably put it in a HttpModule and record the number of hits. Without logging users in, it would be difficult to know what constitutes a user "session" on your site, hence logging number of hits would be easier.

You might need to have a notice on your site to inform users of what information you are capturing.

Working with the ASP.NET Global.asax file, The Global. asax file is the central point for ASP.NET applications. It provides numerous events to handle various application-wide tasks such as user authentication, application start up, and dealing with user sessions. You should be familiar with this optional file to build robust ASP. The Global.asax, also known as the ASP.NET application file, is located in the root directory of an ASP.NET application. This file contains code that is executed in response to application-level and session-level events raised by ASP.NET or by HTTP modules.

If you want to manage visitor on code level you need to start visitor counter under Application_Start method in application configuration file after need to increase the counter on every session.

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();
}

Understanding the Global.asax file: ASP Alliance, The Application_Start event is fired the first time when an application starts. Session_Start. The Session_Start event is fired the first time when a user's session is  The following are some of the important events in the Global.asax file. The purpose of these event handlers is discussed in this section below. The Application_Init event is fired when an application initializes the first time. The Application_Start event is fired the first time when an application starts.

Global.asax and the HttpApplication class, Also, we know the ASP.NET runtime calls Application_Start only once when the application starts. All of these clues seem to imply there would be  Global.asax wasn't inheriting from it, and it's easy to never see this file if the .cs file is present - you have to right-click Global.asax and click View Markup to actually see it. Global.asax: <%@ Application Language="C#" %> Needed to be changed to:

Working with Global.asax file and Application Events in Web , use Global.asax file to Work with Application Events of your . Application_Start​() : Occurs when the application starts, which is the first time it  In my Global.asax file, in the Application_Start event handler, I have some code which logs the fact that the website has started up. This all works fine with Cassini. Since deploying the site to a virtual directory on a test server using IIS6, I am finding there are no log entries being written, and so I'm concluding that the Application_Start

[Solved] Global.asax Application_Start() working in dev environment , If you change the web.config file and save changes it will force the application to be re-started. After that check if the Application_Start() event is  The Global.asax file is in the root application directory. While Visual Studio.NET automatically inserts it in all new ASP.NET projects, it's actually an optional file. It's okay to delete it—if

Comments
  • Thanks Russ, I am storing only browser info for my reference like Browser Name and Major Version. I'll implement HttpModule. Thanks :)