ASP.NET对象速查

1. Request对象

属性名

值类型

说明

ApplicationPath

String

获取请求的资源在网站上的根路径

ContentEncoding

Encoding

设置请求对象的编码

Cookies

httpCookieCollection

客户端发送到服务器的Cookie集合

QueryString

NameValueCollection

当前请求的查询字符串集合

UrlReferrer

url

获取用户由哪个URL跳转到当前页面

2. Response对象

属性名

值类型

说明

Charset

String

表示输出流所使用的字符集

ContentEncoding

Encoding

设置输出流的编码

ContentLength

Int

输出流的字节大小

ContentType

String

输出流的HTTP MINE 类型

Cookies

httpCookieCollection

服务器发送到客户端的Cookie集合

Output

TextWriter

服务器响应对象的字符输出流

OutputStream

Stream

服务器响应对象的二进制输出流

 代码示例:

<table border="1" width="600px">
<tr><td colspan="2" bgcolor="#80ffff">Request</td></tr>
<tr>
<td>ApplicationPath(网站路径)</td>
<td><%=Request.ApplicationPath %></td>
</tr>
<tr>
<td>ContentEncoding(网站编码)</td>
<td><%=Request.ContentEncoding %></td>
</tr>
<tr>
<td>Cookies个数</td>
<td><%=Request.Cookies.Count %></td>
</tr>
<tr>
<td>QueryString个数</td>
<td><%=Request.QueryString.Count %></td>
</tr>
<tr>
<td>UrlReferrer(上一个请求页面)</td>
<td><%=Request.UrlReferrer %></td>
</tr>
<tr><td colspan="2" bgcolor="#80ffff">Response</td></tr>
<tr>
<td>Charset</td><td><%=Response.Charset %></td>
</tr>
<tr>
<td>ContentEncoding(网页编码)</td>
<td><%=Response.ContentEncoding %></td>
</tr>
<tr>
<td>Cookies个数</td>
<td><%=Response.Cookies.Count %></td>
</tr>
<tr>
<td>ContentType</td>
<td><%=Response.ContentType %></td>
</tr>
</table>

3. Server对象

常用方法:

  • Public string MapPath(string path):取指定相对路径在服务器上的物理路径。
  • Public void Execute/Transfer(string path):先执行路径代表的URL,执行完之后再继续执行本页/停止执行当前程序,执行指定的路径所代表的URL。
  • Public string HtmlDecode/HtmlEncode(string s):消除对特殊字符串编码/对特殊字符串编码。
  • Public string UrlDecode/UrlEncode(string s):对Url路径字符串进行解码/编码。

代码示例:

<ul>
<li>Server.MapPath(".")=<%=Server.MapPath(".") %></li>
<li><%=Server.HtmlEncode("<h1>ASP.NET</h1>") %></li>
<li><h1>ASP.NET</h1></li>
<li><%=Server.UrlEncode("<a href=\"http://google.com\">Google</a>") %></li>
<li><a href="http://google.com">Google</a></li>
</ul>

4. Session对象

属性名

值类型

说明

Count

Int

获取Session 中存储的对象的个数

SessionID

String

用于获取Session在服务器上的唯一标识

Timeout

Int

用户设置或获取Session超时时间(分钟)

 代码示例:

string userName;

if (Session["UserName"] != null)
{
userName
= (string)Session["UserName"];
}

5. Cookie对象

 代码示例:

//发送Cookie到客户端
HttpCookie cookie = new HttpCookie("UserName", "cnblog");
Response.Cookies.Add(cookie);

//获取Cookie
string userName;

if (Request.Cookies["UserName"] != null)
{
userName
= Request.Cookies["UserName"].Value;
}

 
6. Application对象

方法和属性与Session对象类似。

7. ViewState对象

代码示例:

<script runat="server">

protected void btnSet_Click(object sender, EventArgs e)
{
ViewState[
"text"] = txtSource.Text;
}

protected void btnGet_Click(object sender, EventArgs e)
{
if (ViewState["text"] != null)
{
txtResult.Text
= (string)ViewState["text"];
}
else
{
txtResult.Text
= string.Empty;
}

}
</script>

<body>
<form id="form1" runat="server">
<div>
<table border="0">
<tr>
<td>要设置的值</td><td><asp:TextBox ID="txtSource" runat="server" /></td>
</tr>
<tr>
<td>获取的值</td><td><asp:TextBox ID="txtResult" runat="server" /></td>
</tr>
<tr>
<td><asp:Button ID="btnSet" runat="server" Text="设置" OnClick="btnSet_Click" /></td>
<td><asp:Button ID="btnGet" runat="server" Text="获取" OnClick="btnGet_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>

posted @ 2011-04-26 14:55  freebsd_pann  阅读(405)  评论(0编辑  收藏  举报