[C #] counter

xiaoxiao2021-03-06  14

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"%>

You are the "% = Application [" counter "]%> bit visitor!

Listing 2 and Listing3 are the Global.asax and Global.asax.cs file code, perform them before performing the WebForm1.aspx file. In the global.asax.cs file, some events and their response code are defined, primarily used to read and write files and values.

Listing 2 ----- Global.asax ----

<% @ Application SRC = "Global.asax.cs" inherits = "counter2.global"%>%>

Listing 3 ----- Global.asax.cs -----

Using system;

Using system.collections;

Using system.componentmodel; using system.Web;

Using system.Web.SessionState;

Using system.io;

Namespace Counter2

{

Public Class Global: System.Web.httpApplication

{

Protected void Application_Start (Object Sender, Eventargs E)

{

uint count = 0;

StreamReader SRD;

// A actual path to obtain the file

String file_path = server.mappath ("counter.txt");

// Open the file to read

SRD = file.opentext (file_path);

While (Srd.Peek ()! = - 1)

{

String str = srd.readline ();

Count = uint32.parse (STR);

}

Object obj = count;

Application ["counter"] = OBJ;

Srd.close ();

}

Protected Void Session_Start (Object Sender, Eventargs E)

{

Application.lock ();

/ / Numerical accumulation, pay attention to the boxing of boxing

Uint Jishu = 0;

Jishu = (uint) Application ["counter"];

Jishu = jishu 1;

Object obj = jishu;

Application ["counter"] = OBJ;

// write data to the file

String file_path = server.mappath ("counter.txt");

Streamwriter Fs = New StreamWriter (file_path, false);

fs.writeline (Jishu);

fs.close ();

Application.unlock ();

}

Protected Void Application_BeginRequest (Object Sender, Eventargs E)

{

}

Protected Void Application_ENDREQUEST (Object Sender, Eventargs E)

{

}

Protected void session_end (Object Sender, Eventargs E)

{

}

Protected Void Application_END (Object Sender, Eventargs E)

{

/ /

UINT JS = 0;

JS = (uint) Application ["counter"];

// Object obj = js;

// Application ["counter"] = js;

// write data to the file

String file_path = server.mappath ("counter.txt");

Streamwriter Fs = New StreamWriter (file_path, false);

Fs.writeLine (JS);

fs.close ();

}

}

}

summary

After the above discussion, a simple homepage counter is completed. Its core is to read and write in text mode.

转载请注明原文地址:https://www.9cbs.com/read-48762.html

New Post(0)