Tutorial on application usage in ASP.

Source: Internet
Author: User
Tags httpcontext
This article mainly introduces to you about the use of application in ASP. C #, before introducing the usage of application, we introduce the use of the session for everyone to refer to the study, the article introduced in very detailed, The friends who need to follow the small series to learn together.

Application Object

The Application object lifetime is as long as the Web application lifetime, and the lifetime is started from the Web application Web page being accessed, and the HttpApplication class object application is automatically created until no page is accessed. The Application object is automatically revoked. Therefore, the variables in the Application object also have the same lifetime, and the variables can be accessed by all pages in the Web application. Therefore, you can establish some global public variables in the Application object, because the values stored in the Application object can be read by all the pages of the application, so the properties of the Application object are also suitable for passing information between Web pages of the application.

Application objects are mainly used for the following purposes:

    • L Store variables that record the number of people online or the total number of visitors to the site.

    • L Store Web site sharing the latest news for all Web pages to update.

    • L record the number or time that the same ad was clicked on a Web page.

    • L Store database data for use by all Web pages.

    • L Communication between different uses, such as multi-user chat room, multi-user games, etc.

The use of ASP. Application is very different from the Session. Let's take a look at the detailed introduction below:

Usage of Session

First, the Session.add name is the same, will not repeat, but cover.


Session.add ("S1", 1); Session.add ("S1", 2);//S1 End with only one value, which is 2.

Second, the name ignores case.


Session.add ("S1", 1); Response.Write (session["S1"]); Value of 1

Third, the Session Add immediately after the value (Remove the same), this is different from Cookie,cookie to wait until the next page.


Session.add ("S1", 1); Response.Write (session["S1"] = = null); False, it is not null

Four, the stored Session data type is an object, preferably converted with convert.


Convert.ToInt32 (session["S1"]);

If you convert to string it is best to use convert.tostring () instead of session["S1". ToString (), because if the Session is null, an error will be made after the method is used.

V. Use the Session in the class.


System.Web.HttpContext.Current.Session

The use of application

Name duplication problem


HTTPCONTEXT.CURRENT.APPLICATION.ADD ("Key1", "value1"); HTTPCONTEXT.CURRENT.APPLICATION.ADD ("Key2", "value2"); HTTPCONTEXT.CURRENT.APPLICATION.ADD ("KEY2", "value3"); Name ignores case int count = HttpContext.Current.Application.Count; 3 x string[] keys = return HttpContext.Current.Application.AllKeys; Key1, Key2, key2string s = (string) HttpContext.Current.Application.Get ("Key2"); value2string s2 = (string) HttpContext.Current.Application.Get (2); Value3

As shown in the code, the results are listed in the notes. It can be seen that application encountered the same key value, it neither error, nor overwrite the previous, but at the same time exist. When you use a key value name to fetch a value, you get the first corresponding value in the same name. If you want to take the back, use index.

If we want to meet the same name, overwrite it, use the following code


HTTPCONTEXT.CURRENT.APPLICATION.ADD ("Key1", "value1");//HttpContext.Current.Application.Add ("Key2", "value2"); String name = "Key2"; object obj = HttpContext.Current.Application.Get (name); if (obj = = null) {//Not present, add directly Httpcontext.curr Ent. Application.add (name, "value2");} else{//exists, cannot call the Add method directly, this will cause two entries of the same name//obj = "value3";//This method does not work httpcontext.current.application[name] = "Value3 ";} Return (string) httpcontext.current.application[name]; Equivalent to the Get method when using []

In the above code, it would not work to modify obj directly, but the following code would work if the object was encountered. Description: This is a knowledge point for C # value references, address references, and application.


((Site) obj). URL = "222"; It's going to work.
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.