Turn: Application and Session in C#

Reprinted from http://www.jb51.net/article/112973.htm

Application object

The lifetime of the Application object is as long as the lifetime of the Web application. The lifetime begins when the Web application page is accessed, and the HttpApplication class object, Application, is automatically created. It ends when no web page is accessed, and the Application object is automatically revoked. So the variables in the Application object also have the same lifetime, and the variables can be accessed by all web pages in the web application. Therefore, some global public variables can be established in the Application object. Since the values ​​stored in the Application object can be read by all web pages of the application, the properties of the Application object are also suitable for transferring information between the web pages of the application.

The Application object is mainly used for the following purposes:

  • l Store variables that record the number of people online or the total number of people visiting the site.
  • l Store the latest news shared by the website for all web pages to update.
  • l Record the number or time of clicks on the same advertisement on a webpage on the website.
  • l Store database data used by all web pages.
  • l Communication between different users, such as multi-user chat rooms, multi-user games, etc.

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

Session usage

1. When the name of Session.Add is the same, it will not be repeated, but overwritten.

Session.Add("s1", 1);
Session.Add( " s1 " , 2 );
 // s1 has only one value in the end, which is 2.

Second, the name ignores case.

Session.Add("s1", 1);
Response.Write(Session["S1"]); // 值为 1

3. The value can be obtained immediately after Session Add (similar to Remove), which is different from Cookie, which has to wait until the next page.

Session.Add("s1", 1);
Response.Write(Session[ " s1 " ] == null ); // False, it is not null

Fourth, the stored Session data type is object, and it is best to convert it with Convert.

    
Convert.ToInt32(Session["s1"]);

If it is converted to string, it is better to use Convert.ToString() instead of Session["s1"].ToString(), because if Session is null, an error will be reported after using the method.

Fifth, use Session in the class.

System.Web.HttpContext.Current.Session

Application usage

duplicate name 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 个
string[] keys = return HttpContext.Current.Application.AllKeys; // key1、key2、key2
string s = (string)HttpContext.Current.Application.Get("key2"); // value2
string s2 = (string)HttpContext.Current.Application.Get(2); // value3

As in the above code, the results are listed in the remarks. It can be seen that Application encounters the same key value, it neither reports an error nor overwrites the previous one, but exists at the same time. When using the key-value name to retrieve the value, the first corresponding value in the same name is retrieved. If you have to take the latter, use index.

If we want to encounter 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)
{
 // Doesn't exist, add 
 HttpContext.Current.Application.Add(name, " value2 " );
}
else
{
 // Exist, the Add method cannot be called directly, which will result in two entries with the same name
  // obj = "value3"; // This method will not work 
 HttpContext.Current.Application[name] = " value3 " ;
}
 
return ( string )HttpContext.Current.Application[name]; // When using [] to get the value, it is equivalent to the Get method

In the above code, directly modifying obj will not work, but if you encounter an object, the following code will work. Description: This is the knowledge point of C# value reference and address reference, which has nothing to do with Application.

((Site)obj).Url = " 222 " ; // works

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324925930&siteId=291194637