Professional Documents
Culture Documents
Cc i tng ca ASP.NET
Ni dung
Gii thiu v cc i tng ca ASP.NET
Response Request Server Application Session
Mt vi v d
Response Object
Response l i tng c to ra tng ng vi mi yu cu ca client i tng Response thng thng dng x l cc HTTP Request, v tr kt qu v cho client
Response Object
Thuc tnh
Charset IsClientConnected Cookies
M t
xc lp b charset s truyn cho bit hin thi client c ang kt ni hay khng cc thng tin v Cookies s c ghi xung client
Response Object
Phng thc
Write WriteFile BinaryWrite Close End Redirect
M t
ghi d liu text ghi d liu t file ghi d liu nh phn ng kt ni ti client Kt thc thi hnh mt trang chuyn client sang mt URL khc
V d
Request Object
Cung cp cc thng tin v yu cu ca client c to ra tng ng vi cc Http Request Dng i tng ny c nhng thng tin client gi (header, query string, cookies, )
Request Object
Thuc tnh
Browser Url Cookies QueryString Form
M t
Cung cp thng tin lin quan n trnh duyt ca client a ch trang gi request cc thng tin v Cookies client s c c ln Ly tham s truyn t client theo phng thc GET Ly tham s truyn t client theo phng thc POST
Cookies
Cookies l mt mu thng tin nh dng lu tr thng tin ca ngi dng trn my tnh. Cookies c th c lu tr tm thi hoc lu tr lu di.
Cookies
Cookie lu tr tm thi : s khng cn gi tr ngay khi ngi dng ri khi web site. Cookie lu tr lu di : vn cn c lu tr trn my ca ngi dng,v Web Server c th c nhng thng tin ny vo nhng ln k tip ngi dng vo web site.
Cookies
Th hin trong ASP.NET l lp HttpCookie Cc thuc tnh ca HttpCookie Name : tn ca Cookie Domain : domain cookie ny thuc v. Expires : xc nh thi gian c hiu lc ca Cookie Value : Gi tr ca Cookie HasKeys : Cookie c tp gi tr con hay khng Values : tp cc gi tr ca Cookie
S dng Cookies
Lnh ghi cookies
Response.Cookies[cookie][(key)|.attribute] = value; cookie: tn bin key : tham s ty chn, c th t nhiu gi tr cho 1 cookie attribute: thuc tnh (domain, path,.)
Ly gi tr cookies
value = Request.Cookies[cookie][(key)|.attribute]
V d
Cookies n Ghi
Response.Cookies["userName].Value = "mike; Response.Cookies["userName].Expires = DateTime.Now.AddDays(1);
c if (Request.Cookies["userName] != null)
Label1.Text = Request.Cookies["userName].Value;
V d
Cookies c cc subkey Ghi
Response.Cookies["userInfo]["userName] = "mike; Response.Cookies["userInfo]["lastVisit] = DateTime.Now.ToString(); Response.Cookies["userInfo].Expires = DateTime.Now.AddDays(1);
c
if (Request.Cookies["userInfo] != null) { Label1.Text = Request.Cookies["userInfo]["userName]; Label2.text = Request.Cookies["userInfo]["lastVisit]; }
V d Xa Cookies
Response.Cookies["userName].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["userInfo].Expires = DateTime.Now.AddDays(-1);
Server Object
Cung cp cc phng thc gip
Chuyn iu khin gia cc trang vi nhau Ly cc thng tin v m li, encode,
Server Object
Cc thuc tnh
MachineName : tn server ScriptTimeout : thi gian time-out ca request
Server Object
Phng thc
MapPath HtmlEncode HtmlDecode UrlEncode UrlDecode Transfer Execute
M t
nh x a ch tng i thnh a ch tuyt i trn server Gi nguyn tag HTML nh dng ni dung theo tag HTML M ha Url theo ASCII Gii m ASCII t Url Ly kt xut t trang khc ri quay li Ly kt xut t trang khc
Application Object
Mt ASP.NET application bao gm tt c cc file, trang web, s kin, module v code trong phm vi mt th mc web o (virtual directory) v cc th mc con ca n i vi mi ASP.NET application, mt Application Object c to ra th hin tnh trng ca ASP.NET application ny Application Object c to khi client yu cu bt k trang no trong application ny
Application Object
Application Object b hy khi
Stop Web Server Server b sp Hy ASP.NET Application
S kin
Application_OnStart Application_OnEnd (global.asax)
Application Object
Application Object cha mt danh sch cc bin trng thi dng chung ca application. Ta c th s dng chng lu tr cc thng tin xuyn sut ng dng Cc bin trng thi ny c lu tr thnh tng cp key-value.
Key : tn trng thi Value : gi tr trng thi
Application[SoLan"] = 0;
Session Object
Mt session (phin lm vic) l mt chui cc thao tc ca ngi dng trn cng mt web application Vi mi phin lm vic ca client, s c mt Session Object c to ra Session Object s cung cp cho ta nhng thng tin v phin lm vic hin hnh ny ca client
Session Object
Session c to ra khi client bt u phin lm vic ca mnh Session s c hy khi Client tng minh thot khi session Sau mt khong thi gian (time-out), client khng c hnh ng lm vic no S kin Session_OnStart Session_OnEnd (global.asax)
Session Object
ngha
Session Object
Cung cp cho ta mt danh sch cc trng thi xuyn sut session ny Cc trng thi ca Session Object cng c t chc trong mt Collections di dng key-value Session[DaDangNhap"] = 0;
Lm th no ngn khng cho ngi dng truy cp vo cc trang web nu cha ng nhp?
1. Khi to gi tr mc nh cho bin Session[IsLogin] = 0 (cha ng nhp)
//global.asax <script language="C#" runat="server"> void Session_OnStart(Object sender, EventArgs E) { Session [IsLogin"] = 0; } </script>
Lm th no ngn khng cho ngi dng truy cp vo cc trang web nu cha ng nhp?
2. To trang Login.aspx cho php ngi dng ng nhp Nu kim tra thng tin ng nhp khng ng th hin th thng bo yu cu ng nhp li. Ngc li, nu NG th dng mt (hoc nhiu) bin Session lu trng thi login thnh cng li.
//Login.aspx.cs void btnLogin_Click(Object Src, EventArgs E){ if (Thong tin dang nhap dung) Session [IsLogin"] = 1; else Response.Write(Vui lng nhp li!); }
Lm th no ngn khng cho ngi dng truy cp vo cc trang web nu cha ng nhp?
3. Trong tt c cc trang mun bo mt, phi thm on m sau kim tra ngi dng login hay cha, nu cha th redirect li trang login.aspx
//MyForm.aspx.cs void Page_Load(Object Src, EventArgs E){ int nDaDangNhap = (Int32) Session [IsLogin "]; if (nDaDangNhap == 0) Response.Redirect(Login.aspx); }
Lm th no ngn khng cho ngi dng truy cp vo cc trang web nu cha ng nhp?
4. To x l khi ngi dng logout Reset trng thi login l cha ng nhp