Detailed description of the Application instance for transferring values between ASP. NET pages, asp. netapplication

Source: Internet
Author: User

Detailed description of the Application instance for transferring values between ASP. NET pages, asp. netapplication

Application

Application variables are valid throughout the Application lifecycle, similar to using global variables, so they can be accessed on different pages. The difference between it and Session variables is that the former is a global variable shared by all users, and the latter is a global variable unique to each user.

For example:

The counter variable for Website access generally uses the Application variable. When multiple requests access this variable, you can operate on it. This variable can be directly used by all pages of the Application.

The account name you log on to generally uses the Session variable. When multiple requests are accessed, they have their own Session variables and can only operate on their own Session variables, this variable is directly used on each page of the application to obtain basic user information. (The Session will be organized in the next article)

Advantages: 1. Simple to use and consume less server resources.

2. Not only simple data can be passed, but also objects can be passed.

3. The data size is unlimited.

Disadvantage: 1. As a global variable, it is prone to misoperations. Therefore, application is generally unavailable for variables used by a single user.

Usage: 1. Create the name and value you need to pass in the Code on the Source Page to construct the Application variable:Application["Nmae"]="Value(Or Object)";

2. The code on the target page uses the Application variable to retrieve the passed value.Result = Application["Nmae"]

Note: The lock and unlock methods are often used to lock and unlock, in order to prevent concurrent modification.

Website access example: You can use the Global. asax file to count the total traffic of a website without adding database fields!

(1) Global. asax

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. security; using System. web. services. description; using System. web. sessionState; namespace WebApplication {public class Global: System. web. httpApplication {protected void Application_Start (object sender, EventArgs e) {Application. lock (); Application ["count"] = 0; // Application. set ("count", 0)/Applicatio N. Add ("count", 0) initializes the variable, which is equivalent to setting count to 0. Application ["online"] = 0; Application. unLock ();} protected void Session_start (object sender, EventArgs e) {Application. lock (); Application ["count"] = (int) Application ["count"] + 1; Application ["online"] = (int) application ["online"] + 1; Application. unLock ();} protected void Session_end (object sender, EventArgs e) {Application. lock (); Session. abandon (); // when a session ends, the Application ["online"] = (int) Application ["online"]-1; Application. unLock ();}}}

(2) Index. aspx. cs

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace WebApplication {public partial class Index: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {Response. write ("<br/> total access count:" + Application ["count"]); Response. write ("<br/> current number of online users:" + Application ["online"]) ;}}

(3) Web. config (put "<sessionState mode =" InProc "timeout =" 1 "cookieless =" false "/>" above "</system. web>)

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- For more information about how to configure ASP. NET applications, visit https://go.microsoft.com/fwlink/?LinkId=169433-- > <Configuration> <system. web> <compilation debug = "true" targetFramework = "4.6.1"/> 

(4) view the effect.

1. Run the program to view it in the browser. Refresh the page one minute later;

2. Copy the address in the address bar and put it in another browser to view the effect;

3. Use the same browser to create a new stealth window. Copy the address in the address bar and view it.

Summary

The above is a small series of ASP.. NET page. If you have any questions, please leave a message. The editor will reply to you in time. Thank you very much for your support for the help House website!

Related Article

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.