当前位置:首页 > .NET内置对象之Request对象

.NET内置对象之Request对象

点击次数:1024  更新日期:2010-12-29
\n

Request对象是HttpRequest类的一个实例,它提供对当前页请求的访问,其中包括请求标题、Cookie、客户端证书、查询字符串等,你可以使用此类来读取浏览器已经发送的内容。
新建一个网站,包括两个网页,代码如下:
1、Default.aspx代码:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Request对象</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<br />
<asp:Button ID=”Button1″ runat=”server” Text=”读取与传值” onClick=”Button1_Click” /></div>
</form>
</body>
</html>
Default.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


\n

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Application["id"] = TextBox1.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect(“Default2.aspx?id=” + TextBox1.Text.Trim() + “”);
}
}
2、Default2.aspx代码:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default2.aspx.cs” Inherits=”Default2″ %>


\n

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>


\n

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>读取与传值</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
接受页面传值,请注意浏览器地址栏!!!
<br />
<asp:Label ID=”Label1″ runat=”server” Height=”28px” Text=”Label” Width=”326px”></asp:Label></div>
</form>
</body>
</html>
Default2.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


\n

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Page.Request.QueryString["id"].ToString();
Response.Write(Request.Browser.Platform);
Response.Write(“<br>”);
Response.Write(Request.MapPath(“Default2.aspx”));
Response.Write(“<br>”);
Response.Write(Request.UserHostAddress);
Response.Write(“<br>”);
Response.Write(Request.Url.OriginalString);
Response.Write(“<br>”);
Response.Write(Request.RawUrl);
Response.Write(“<br>”);
Response.Write(Request.QueryString);
Response.Write(“<br>”);
Response.Write(Request.HttpMethod);
Response.Write(“<br>”);
Response.Write(Request.UserHostAddress);
Response.Write(“<br>”);
Response.Write(Request.UserAgent);
Response.Write(“<br>”);
}
}

\n