ASP. NET C Tutorial on Usage of Application
- 2021-12-12 09:28:49
- OfStack
Application object
The lifetime of the Application object is as long as that of the Web application. The lifetime starts when the Web application web page is accessed, and the HttpApplication class object Application is automatically created until no web page is accessed, and the Application object is automatically revoked. Therefore, the variables in the Application object have the same lifetime, and the variables can be accessed by all web pages in the Web application. Therefore, some global common 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 program, the attributes of the Application object are also suitable for passing information between web pages of the application program.
Application objects have the following main purposes:
The usage of Application for ASP. NET is very different from Session. Let's take a look at the detailed introduction:
Usage of Session
1. When Session. Add has the same name, it is not duplicated, but overwritten.
Session.Add("s1", 1);
Session.Add("s1", 2);
// s1 In the end, only 1 A value, that is, 2 .
2. Name ignores case.
Session.Add("s1", 1);
Response.Write(Session["S1"]); // Value is 1
3. The value is immediately available after Session and Add (the same applies to Remove), unlike Cookie, which is not available until the next page.
Session.Add("s1", 1);
Response.Write(Session["s1"] == null); // False , it is not for null
4. The stored Session data type is object, preferably converted with Convert.
Convert.ToInt32(Session["s1"]);
If converting to string, it is better to use Convert. ToString () instead of Session ["s1"]. ToString (), because if Session is null, an error will be reported by using the post-method.
5. Use Session in your 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 Ignore case
int count = HttpContext.Current.Application.Count; // 3 A
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, and it neither reports an error nor overrides the previous one, but exists at the same time. When using the key value name to take a value, the first corresponding value in the same name is taken. If you have to take the latter, use index.
If we want to meet the same name, we can override it with 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)
{
// Does not exist, add it directly
HttpContext.Current.Application.Add(name, "value2");
}
else
{
// Exists and cannot be called directly Add Method, which causes two identical name Entries of
// obj = "value3"; // This method won't work
HttpContext.Current.Application[name] = "value3";
}
return (string)HttpContext.Current.Application[name]; // Use [] When taking a value, it is equivalent to Get Method
In the above code, it is not feasible to modify obj directly, but if you encounter objects, the following code is feasible. Description: This is C # value reference, address reference knowledge points, and Application has nothing to do.
((Site)obj).Url = "222"; // It will work
Summarize