ASP.NET内置对象

ASP.NET的基本内置对象包括Response、Request、Application、Session、ViewState、Cookie 和 Server对象。

Response对象

用于将数据从服务器发送回浏览器。他允许将数据作为请求的结果发送到浏览器中,并提供有关响应的信息。还可以用来在页面中输入数据、在页面中跳转,并传递各个页面的参数。

1 Response对象通通过使用Write 或 WriteFile方法在页面上输出数据。

2 Response对象的Redirect方法实现页面的重定向。

string name = "Yuan";
string age = "26";
Response.Redirect("~/welcome.aspx?Name=" + name + "&Age=" + age + "");

3 输出二进制图像

FileStream fs = new FileStream(Server.MapPath("pic.bmp"), FileMode.Open);
long fileSize = fs.Length;
byte[] Buffer = new byte[(int)fileSize];
fs.Read(Buffer, 0, (int)fileSize);
fs.Close();
Response.BinaryWrite(Buffer);

4

Response.Write("<Script>alert('hello world!')</Script>");
Response.Write("<Script>window.close();</Script>");

Request对象

用于检索从浏览器想服务器发送的请求中的信息,提供对当前页面的访问,包含标题、Cookie、客户端证书等。

1 Request对象通过Params 和 QueryString 属性获取页面间传送的值。

name = Request.QueryString["name"] == null ? string.Empty : Request.QueryString["name"].ToString().Trim();

2 常用属性

  Request.UserHostAddress;
  Request.UserHostName;
  Request.PhysicalApplicationPath;

Cookie对象

用于保存客户端浏览器请求的服务器页面或客户端信息。

Cookie详见: http://www.cnblogs.com/fish-li/archive/2011/07/03/2096903.html

Seesion对象

用于存储在多个页面调用之间特定的用户信息,只针对单一网站使用者,不同客户端无法相互访问。

Seesion详见:http://www.cnblogs.com/knowledgesea/archive/2012/11/20/2779185.html

 Application

Application对象定义的变量是应用程序级变量,为全局变量。变量可以在Global.asax文件或是aspx页面中声明。

详见:http://www.cnblogs.com/Fskjb/archive/2010/04/26/1721624.html

Server

提供最服务器上的方法和属性的访问,用于访问服务器上的资源。

ViewState

ASP.NET中用来保存WEB控件回传时状态值一种机制,是由ASP.NET页面框架管理的一个隐藏的窗体字段。
用来保存页面中的各种变量,甚至是对象,ViewState相当于页面级的Session。

相关文章:

1.ASP.NET内置对象 http://www.cnblogs.com/MyBeN/archive/2011/03/23/1992591.html

2.Asp.Net页面生命周期 http://www.cnblogs.com/xhwy/archive/2012/05/20/2510178.html

原文地址:https://www.cnblogs.com/YuanSong/p/3037899.html