This article describes how to use ASP. NET technology to create homepage counters.

Source: Internet
Author: User
Design Concept

The core task of a counter is to record the number of visits and read data records conveniently. The hosts file is used to store access records, including global. asax and Global. asax. cs. These two files are core files and are mainly responsible for responding to events and reading and writing files. Therefore, a program must have functions such as opening a file, reading a file, accumulating a number, and writing a file. At the same time, you should also note that when adding a value, it cannot be written as in ASP.

Application ("counter") = Application ("counter") + 1

Because the numeric type cannot perform mathematical operations with the object. After thinking above, we can basically write code, but before writing, we should also understand the following knowledge.

Related Knowledge

1. Global. asax File

The global. asax file is also called an ASP. NET application file, which is usually stored in the root directory. The code in this file does not generate a user interface or correspond to 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 events

The application object comes from the httpapplictionstat class. It can share public information between multiple requests and connections, or act as an information transmission pipeline between request connections. The life cycle of this object starts when IIS starts to run and when someone starts to connect, it ends when IIS is closed or no one is connected for several times (20 minutes by default ). When the life cycle of the Application object starts, the application_start event is started. When the life cycle of the Application object ends, the application_end event is started.

3. Session objects and events

Session objects have similar events as application: session_start and session_end events. When a new user accesses the application, the session_start event is triggered immediately. When a user stops access or the program executes the session. Abandon method, the session_end event is triggered.

4. Comparison of application and session objects

The Session object is somewhat similar to the application object, but its scope is limited. The application object is effective for all users, while the session object is opposite. each user has its own session object. its lifecycle starts from the server's response to the user request page, it is terminated when the user disconnects from the server. The application object does not trigger an event when a new user request is like a session object. The application object event is only triggered once, that is, the first request of the first user. An application_end event must occur after the session_end event. The application_end event is triggered only when the server stops working or the application_end event is uninstalled.

Procedure

First, create a counter.txt file. open the file and enter an integer greater than 0 as the initial value of the access record.

We can officially compile the counter program.

Listing 1 is webform1.aspx, which is mainly used to display records of the number of accesses read from the file. Because the application object is valid throughout the application lifecycle, it can be accessed on different pages, just as convenient as using global variables.

In the code, <% = application ["counter"] %> is used to indicate the number of visits.

The program code is as follows:

Listing1 ----- webform1.aspx -----

<% @ Page Language = "C #" src = "webform1.aspx. cs" inherits = "counter1.webform1" %>

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">

<HTML>

<Head>

<Meta name = "generator" content = "Microsoft Visual Studio 7.0">

<Meta name = "code_language" content = "C #">

</Head>

<Body>

<Form ID = "form1" method = "Post" runat = "server">

<Font face = ""> you are the <% = application ["counter"] %> visitor! </Font>

</Form>

</Body>

</Html>

Listing 2 and listing3 are the code of the global. asax and Global. asax. CS files. They are executed before the webform1.aspx files are executed. Some events and response code are defined in the Global. asax. CS file, which is mainly used to read and write files and accumulate 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;

// Obtain the actual path of the file

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

// Open the file for reading

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 ();

// Value accumulation. Note that boxing is used here)

Uint jishu = 0;

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

Jishu = jishu + 1;

Object OBJ = jishu;

Application ["counter"] = OBJ;

// Write data records to files

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)

{

// Packing

Uint JS = 0;

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

// Object OBJ = JS;

// Application ["counter"] = JS;

// Write data records to files

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

Streamwriter FS = new streamwriter (file_path, false );

FS. writeline (JS );

FS. Close ();

}

}

}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.