Foreword
The homepage counter is used to record components that have been accessed by the page. It is a relatively simple feature, but there are many practical problems to consider it. For example, how to store records? If the server is turned off, is it lost? Waiting for us to consider.
The current counter is mainly made of ASP technology, and the method is very simple. However, there are very few counters using ASP.NET technology, the reason is that the ASP.NET technology has not been officially released, and the server supports .NET's server is rarely caused. This article tells how to make a homepage counter using ASP.NET technology.
Design Concept
The core of the counter is to find a way to record the number of visits and can easily read the data records. In this application, it is intended to establish four files, one is WebForm1.aspx, mainly used to display access times, a counter.txt file is used to store access times record, and Global.asax and Global.asax.cs, These two files are core files, mainly responsible for responding events and reading and writing files. Therefore, the program must have functions that open files, read files, accumulation values, write files. At the same time, it is necessary to pay attention to: When performing numerical accumulation, it cannot be written like the ASP.
Application ("counter") = Application ("counter") 1
Because numeric types cannot be mathematically computing with objects.
After the above thinking, we can basically write code, but before completing the preparation, we should understand the following related knowledge.
related information
Global.asax file
Global.asax files are also known as the ASP.NET application file, which is generally placed under the root directory. The code in this file does not generate the user interface, and does not request a single page request. It is mainly responsible for processing Application_Start, Application_end, Session_Start, and Session_end events.
2. Application object and its event
The Application object is from the HTTPAPPLICTIONSTAT class. It can share public information between multiple requests and connectivity, or act as a pipe transfer between the respective request connections. The life cycle of this object starts from IIS to start running and someone starts to connect, terminating unconnected in IIS close or within several times (default for 20 minutes). When the lifecycle of the Application object, the Application_Start event will be activated. The Application_end event will be started when the life cycle of the Application object is.
3. Session object and its event
The Session object has an event similar to Application: Session_Start and session_end events. When there is a new user accessing the application, it will immediately trigger the session_start event. When a user stops access or executes the session.abandon method, the session_end event is triggered.
4. Application and session objects Compare
The session object is similar to the Application object, but its scope has a greater restriction. Application objects are valid for all users, while the Session objects are opposite, each user has its own session object, its lifecycle starts the server to generate the corresponding page of the user, terminate the connection to the user disconnected and server . The Application object does not like the Session object. When a new user request triggers an event, the Application object is only triggered, which is the first request in the first user. An Application_END event will certainly happen after the session_end event, the Application_end event is only triggered when the server stops working or the Application_end event uninstall. Program part
First create a text file counter.txt, open the file to enter an integer greater than 0 as an initial value of the access record.
Below we can formally write a counter program.
Listing 1 is WebForm1.aspx, which is mainly used to display records of access times read from the file. Since the Application object is valid in the entire application lifecycle, it can be accessed in different pages, just as the global variable is as convenient.
In the code, use <% = Application ["counter"]%> to indicate the number of access times.
The program code is as follows:
Listing1 ----- Webform1.aspx -----
<% @ Page language = "c #" src = "Webform1.aspx.cs" inherits = "counter1.webform1"%>
HEAD>