总结——ASP.NET 对象
ASP.NET六大对象是ASP.NET的基础,也是重要的知识,现总结如下。
Response对象
Request对象
理解:
如: URL: <a href="test.aspx?name=Frankie&sex=男">URL1</a>
string name = Request.QueryString["name"]; string sex = Request.QueryString["sex"];
如: 表单: <form action="1.aspx" method="post" id="form1"> <p>姓名<input type="text" size="20" name="Name" /></p> <p>兴趣<input type="text" size="20" name="Love" /></p> <p><input type="submit" name="B1" value="提交" /></p> </form>
string name = Request.Form["Name"]; string hobby = Request.Form["Hobby"]; |
Application对象
理解关键:所有用户都共用一个Application对象,当网站服务器一开,就创建了Application对象,所有的用户都可以对Application对象进行修改。
应用:聊天室、网页计数器等。
聊天室登录界面(前台代码login.aspx):
登录(后台代码login.aspx.cs):
显示界面(Chat.html):分为上下两个框架,一个用来显示聊天内容,一个用来发送信息。模拟QQ聊天界面。
发送消息窗口(Message.aspx):
后台代码(Message.aspx.cs):这是最重要的地方。
显示消息界面(Display.aspx):
完毕! |
自制网页计数器前台页面(counter.aspx):
后台页面(counter.aspx.cs):很重要。
完毕! |
Server对象
Server对象提供对服务器上的方法和属性进行访问。例如对URL或者HTML编码成字符串,获奖虚拟路径映射到物理路径以及设置脚本的超时期限等。
向浏览器输出HTML代码:protected void Page_Load(object sender, EventArgs e) { Response.Write(Server.MachineName+Server.HtmlEncode(" Hello world;(<h1>Welcome.</h1>)")); Response.Write("<br />"); Response.Write("Hello world;(<h1>Welcome.</h1>)"); }
显示效果如下:
|
取得文件路径:Server.MapPath()的用途是把"网络路径"转换为Server机器上的实际路径。
GetMapPath.aspx.cs <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> Server.MapPath("/") 传回:<%=Server.MapPath("/") %> <br /> Server.MapPath("/abc.txt") 传回:<%=Server.MapPath("/abc.txt") %> <br /> </div> </form> </body> </html> |
Session对象
Session其实就是访问者从到达某个特定网页到离开为止的那段时间,每个访问者都会获得一个单独的Session。
用Session实现电子商务网站购物车功能
网络购物车
球店:ballShop.aspx<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" action="ballShop.aspx" method="post" runat="server"> <h1><p>各种球大甩|卖,一律8块:</p></h1> <p><input type="checkbox" name="basketball" value="basketball" />basketball</p> <p><input type="checkbox" name="football" value="football" />football</p> <p><input type="checkbox" name="volleyball" value="volleyball" />volleyball</p> <p><input type="submit" name="submit" value="submit" /> <input type="reset" value="reset" name="reset" /> <a href="meatShop.aspx">Buy Another</a> <a href="Cart.aspx">查看购物车</a> </p> </form> </body> </html>
后台代码:ballShop.aspx.csprotected void Page_Load(object sender, EventArgs e) { if (Request["submit"] == "submit") { Session["basketball"] = Request["basketball"]; Session["football"] = Request["football"]; Session["volleyball"] = Request["volleyball"]; } }
购物车:Cart.aspx<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>User Name</title> </head> <body> <form id="form1" runat="server"> <h1><p>你选择的结果是:</p></h1> <div align="center"> <center> <%Response.Write(Session["pork"]);%><br /> <%Response.Write(Session["beef"]);%><br /> <%Response.Write(Session["mutton"]);%><br /> <%Response.Write(Session["basketball"]);%><br /> <%Response.Write(Session["football"]);%><br /> <%Response.Write(Session["volleyball"]);%> </center> </div> </form> </body> </html>
|
Cookie对象
写入Cookie:writeCookie.aspx.csprotected void Page_Load(object sender, EventArgs e) { HttpCookie myCookie = new HttpCookie("user"); myCookie.Value = "Hello World"; Response.Cookies.Add(myCookie); //Response.Write(myCookie.Value); }
读取Cookie:readCookie.aspx.csprotected void Page_Load(object sender, EventArgs e) { string myCookie = Request.Cookies["user"].Value; Response.Write(myCookie); }
|
完毕!