ASP.NET C#中Application的用法教程
作者:daisy 时间:2023-09-16 14:09:32
Application对象
Application对象生存期和Web应用程序生存期一样长,生存期从Web应用程序网页被访问开始,HttpApplication类对象Application被自动创建,直到没有一个网页被访问时结束,Application对象被自动撤销。因此Application对象中的变量也有相同生存期,并且变量可以被Web应用程序中的所有网页访问。因此,可以在Application对象中建立一些全局的公用变量,由于存储在Application对象中的数值可以被应用程序的所有网页读取,所以Application对象的属性也适合在应用程序的网页之间传递信息。
Application对象主要有以下用途:
l 存储记录在线人数或访问网站总人数的变量。
l 存储网站共用最新消息,供所有网页更新。
l 记录网站中个网页同一条广告被点击的次数或时间。
l 存储供所有网页使用的数据库数据。
l 不同用之间通讯,例如多用户聊天室,多用户游戏等
关于 ASP.NET 的 Application 的用法,与 Session 非常不同。下面来看看详细的介绍:
Session的用法
一、Session.Add 名称相同时,不会重复,而是覆盖。
Session.Add("s1", 1);
Session.Add("s1", 2);
// s1 最终只有一个值,就是 2。
二、名称忽略大小写。
Session.Add("s1", 1);
Response.Write(Session["S1"]); // 值为 1
三、Session Add 后立刻就可取到值(Remove 同理),这点不同于 Cookie,Cookie 要等到下个页面才有。
Session.Add("s1", 1);
Response.Write(Session["s1"] == null); // False,它不为 null
四、存储的 Session 数据类型为 object,最好用 Convert 转换。
Convert.ToInt32(Session["s1"]);
如果转换为 string 最好用 Convert.ToString(),而不是 Session["s1"].ToString(),因为如果 Session 为 null,用后法就会报错。
五、在类中使用 Session。
System.Web.HttpContext.Current.Session
Application 的用法
名称重复问题
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
如上代码,结果我们在备注中列出了。可以看出 Application 遇到键值相同,它既不报错,也不覆盖之前的,而是同时存在。用键值名称去取值时,取到的是同名中第一个对应的值。如果非要取后面的,就用 index。
如果我们要遇相同 name,就覆盖,可用下面的代码
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 方法
上面代码中,直接修改 obj 是行不通的,但是遇到对象的话,如下代码是行得通的。说明:这是 C# 值引用、地址引用的知识点,与 Application 无关。
((Site)obj).Url = "222"; // 行得通
总结
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中的where泛型约束介绍
Android输入框实时模糊搜索效果的示例代码
![](https://img.aspxhome.com/file/2023/0/90120_0s.gif)
简单阐述一下Java集合的概要
![](https://img.aspxhome.com/file/2023/0/58300_0s.png)
java实现文件的断点续传
![](https://img.aspxhome.com/file/2023/4/59544_0s.jpg)
SpringBoot项目集成日志的实现方法
![](https://img.aspxhome.com/file/2023/0/86050_0s.png)
C# 拷贝数组的几种方法(总结)
java arrayList遍历的四种方法及Java中ArrayList类的用法
Lucene实现索引和查询的实例讲解
![](https://img.aspxhome.com/file/2023/3/118903_0s.png)
SpringBoot接口加密解密统一处理
Java中比较抽象类与接口的异同
输出的文本实现对齐的方法(超简单)
![](https://img.aspxhome.com/file/2023/1/66581_0s.jpg)
在Android app中实现九(n)宫格图片连续滑动效果
![](https://img.aspxhome.com/file/2023/4/110704_0s.jpg)
如何利用反射构建元数据查看器
Java class文件格式之方法_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/3/99893_0s.png)
Android 面试精华题目总结
Android布局中gravity与layout_gravity属性说明
![](https://img.aspxhome.com/file/2023/1/93321_0s.jpg)
java抓取网页或文件中的邮箱号码
![](https://img.aspxhome.com/file/2023/9/62119_0s.jpg)
详解Struts2拦截器机制
![](https://img.aspxhome.com/file/2023/8/77358_0s.png)
Spring+EHcache缓存实例详解
![](https://img.aspxhome.com/file/2023/2/77932_0s.jpg)