Home  >  Article  >  Backend Development  >  Tutorial on how to use Application in ASP.NET C#

Tutorial on how to use Application in ASP.NET C#

巴扎黑
巴扎黑Original
2017-05-14 17:07:562290browse

This article mainly introduces you to the usage of Application in ASP.NET C#. Before introducing the usage of Application, I first introduce the usage of Session for your reference and study. The introduction in the article is very detailed. Friends who need it Let’s follow the editor to learn together.

Application object

The Application object lifetime is as long as the Web application lifetime. The lifetime is determined from the Web application web page. When the access starts, the HttpApplication class object Application is automatically created. When no web 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 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 web pages of the application.

Application object mainly has the following purposes:

  • l Store and record the number of people online or the total number of people visiting the website Variables.

  • l Stores the latest news shared by the website for all web pages to update.

  • l Record the number of times or time the same advertisement is clicked on each web page of the website.

  • l Stores database data used by all web pages.

  • l Communication between different applications, such as multi-user chat rooms, multi-user games, etc.

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

Usage of Session

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


Session.Add("s1", 1);
Session.Add("s1", 2);
// s1 最终只有一个值,就是 2。

2. Names ignore case.


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

3. The value can be obtained immediately after Session Add (the same is true for Remove). This is different from Cookie, which has to wait until the next page. Only then.


Session.Add("s1", 1);
Response.Write(Session["s1"] == null); // False,它不为 null

4. The stored Session data type is object, and it is best to use Convert for conversion.


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

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

5. Use Session in the class.


System.Web.HttpContext.Current.Session

Usage of Application

Duplicate name problem


HttpContext.Current.Application.Add("key1", "value1");
HttpContext.Current.Application.Add("key2", "value2");
HttpContext.Current.Application.Add("KEY2", "value3"); // name 忽略大小写

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

The code is as above, and the results are listed in the remarks. It can be seen that when Application encounters the same key value, it neither reports an error nor overwrites the previous one, but exists at the same time. When you use the key name to get the value, you get the first corresponding value with the same name. If you have to get the last one, use index.

If we want to overwrite the same name, we can 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)
{
 // 不存在,直接添加
 HttpContext.Current.Application.Add(name, "value2");
}
else
{
 // 存在,不能直接调用 Add 方法,这样会造成两个相同 name 的条目
 // obj = "value3"; // 这种方法行不通
 HttpContext.Current.Application[name] = "value3";
}

return (string)HttpContext.Current.Application[name]; // 用 [] 取值时,等同于 Get 方法

In the above code, directly modifying obj will not work, but when If you want to get the object, the following code will work. Note: This is a knowledge point about C# value reference and address reference, and has nothing to do with Application.


((Site)obj).Url = "222"; // 行得通

The above is the detailed content of Tutorial on how to use Application in ASP.NET C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn