Asp. NET of application detailed

Source: Internet
Author: User

Application in ASP .

1. Application is used to save information that is common to all users. In the ASP era, using application is an ideal choice if the data to be saved is not or rarely changed during the lifetime of the application. But in ASP. NET development environment, programmers usually put similar configuration data in the Web. config. If you want to use application, be aware that all write operations are done in the Application_OnStart event in the Global.asax file.

The following code is set in the Global.asax file

Application.Lock ();

application["UserId"]= "Hello kitty";

Application.UnLock ();

The following is a call to application in the page

String userid=application["UserId"]. ToString ();

2, Application characteristics:

1. Information size is arbitrary size

2. Apply to entire application/all users

3. Save on server side

4. Scope and save time is the lifetime of the entire application

3. If you use the Application object in your application, one of the issues to consider is that any write operation is done in the Application_OnStart event in the global file. Although the Application.Lock () and Application.UnLock () methods are used to avoid synchronization, because of the serialization of requests to the Application object, there is a serious performance bottleneck when the Web site accesses a large amount of traffic. Therefore, it is best not to save large collections of data in this object.

4, the following use application to achieve online user statistics, record the number of people and total number of visitors to the site.

 usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.ComponentModel;usingSystem.IO;usingSystem.Web.SessionState;/// <summary>///Summary description of Global/// </summary> Public classglobal:system.web.httpapplication{PrivateSystem.ComponentModel.IContainer component =NULL;//necessary designer Variables    PrivateFileStream FileStream; PrivateStreamReader reader;//read stream of characters    PrivateStreamWriter writer;//Write character Stream        PublicGlobal () {}/// <summary>    ///Website program Launch/// </summary>    /// <param name= "Sender" ></param>    /// <param name= "E" ></param>    protected voidApplication_Start (ObjectSender,eventargs e) {application["CurrentUser"] =0;//Initializing online users//loads the file stream and creates a file if the file does not existFileStream=NewFileStream (Server.MapPath ("~/count.txt"), FileMode.OpenOrCreate); Reader=NewStreamReader (FileStream);//using streams to read filesapplication["AllUser"] = Convert.ToInt32 (reader. ReadLine ());//to deposit records from a file in the total number of accessesReader. Close ();//Close the stream    }    /// <summary>    ///session start, when users visit the site, online users +1, total visits +1/// </summary>    /// <param name= "Sender" ></param>    /// <param name= "E" ></param>    protected voidSession_Start (Objectsender, EventArgs e) {Application.Lock ();//lock to prevent simultaneous entryapplication["CurrentUser"] = (int) application["CurrentUser"] +1; application["AllUser"] = (int) application["AllUser"] +1; FileStream=NewFileStream (Server.MapPath ("~/count.txt"), FileMode.OpenOrCreate, FileAccess.Write); //Write Streamwriter=NewStreamWriter (FileStream); //Gross position access number is written to the Count.txt file againwriter. WriteLine (application["AllUser"].        ToString ()); Writer. Close ();//Close Write StreamApplication.UnLock ();//Unlock Lock    }    /// <summary>    ///Session End/// </summary>

Asp. NET of application detailed

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.