ASP.NET 提供的7大内置对象:
- Response
服务器端将数据作为请求的结果发送到浏览器端(输出) - Request
浏览器端对当前页请求的访问发送到服务器端(输入) - Application 存储跨网页程序的变量或对象,中止于停止IIS服务(公用变量和对象)
- Session
存储跨网页程序的变量或对象,中止于联机离线或有效时间(单一用户对象) - Server
定义一个与Web服务器相关的类提供对服务器上方法和属性的访问 - Cookie
保存客户端浏览器请求的服务器页面,存放保留非敏感用户信息 - Cache
Web应用程序的缓存
下面的例子就来写个小程序,学习下使用最多的内置对象session和request,下面看下界面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="usenzdx._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>无标题页</title> </head> <body> <form id="Form1" runat="server" action="servlet/check.ashx" method="post"> 用户名:<input type="text" name="name" id="name"/><br /><br /> 密码:<input type="password" name="password" id="password"/><br /><br /> <input type="submit" value="登录"/> </form> </body> </html>
下面写个一般处理程序来处理用户登录
using System; using System.Collections; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.Web.SessionState; namespace usenzdx.servlet { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class check : IHttpHandler,IReadOnlySessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //取得用户名和密码 string name = context.Request["name"]; string password = context.Request["password"]; // string path1 = context.Response.Write(context.Request.MapPath()); if (name.Equals("tom") && password.Equals("123456")) { context.Response.Write(context.Server.MapPath("/")); context.Response.Write("</br>"); context.Session["username"] = name; context.Server.Transfer("/manage/success.aspx"); } else { context.Response.Redirect("/manage/failure.aspx"); } } public bool IsReusable { get { return false; } } } }
这里需要注意,首先要引入System.Web.SessionState,之后类要继承IReadOnlySessionState,否则会报下面的错误。
下面来看下运行的效果图,这里我也打印了下站点根目录