Applicatin、 server、 session 、cookies对象的简单使用方法
Applicatin 对象
能够记录一组对象,对各个会话都是共享的,为了防止并发,我们用Application.Lock
如:
Application.Lock();
Response.Write(Application["counter"]);
Application.UnLock();
Applicatin 可以存储任何形式的对象(object),如 数组 等
string[] a = new string[3];
a[0]="工人";
a[1]="工人1";
a[2]="工人2";
Application.Add("b", a);
读取:
string[] c = (string[])Application["b"];
可以不同用户之间共享信息等,可用于统计在线用户情况等,相当于整个站点的“全局变量”
在 global.asa中 有 事件
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Application["counter"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
======================================================================
server 对象
server 对象用来获得服务器的信息等;
execute 属性:在服务器端执行另外一页面,然后返回当前页面
Transfer属性:在服务器端执行另外一页面,不返回当前页面,同 response.redirect 一样。
HtmlEncode 属性:对html 代码进行编码,输出纯文本字符串(html 代码不起作用)
UrlEncode 属性:对URL 进行编码,防止页面跳转时候值丢失;
如 Response.Redirect("index.aspx?a=a&b"),由于 ”& “ 的原因传到 index.aspx的参数只是 a
但是这样 Response.Redirect("index.aspx?a="+Server.UrlEncode("a&b")) 传到 index.aspx的值只是 a &b
MapPath 属性 用于获得服务器文件的绝对路径;
======================================================================
session 对象
基本和ASP中用法相同,在此不具体介绍了 session["a"]
-----------------------------------------------------------------------------------------------------------
cookies对象
保存的 cookies名字为 EmailRegister
HttpCookie EmailRegisterCookie = new HttpCookie("EmailRegister");
EmailRegisterCookie.Value = FanEmail;
EmailRegisterCookie.Expires = DateTime.Now.AddSeconds(20);///////// 保存20秒
Response.Cookies.Add(EmailRegisterCookie);//保存
取回 cookies 值用 Request.Cookies["EmailRegister"]