You are on page 1of 65

CHNG II:

CC LP S DNG
TRONG ASP.NET
L thuyt : 3 tit
Thc hnh : 3 tit

Lp Http Request

Request l mt th hin ca lp
HttpRequest, Browser dng Request
object gi thng tin cn thit ti Server
Data

Lp Http Request

i tng Request nhn tt c gi tr m


trnh duyt ca client gi n server
thng qua HTTP request.

Request i din cho Client khi yu cu


trang Web, Server s dng va Response
va Request p ng yu cu hay i
hi thng tin t Client.

Lp Http Request
Cc thuc tnh

RequestType: tr v phng thc


truyn d liu t client n server gm 2
phng thc POST v GET
C php
valMethode=Request.RequestType;

Lp Http Request
1.

Tp hp Form (Form collection )


Form collection c s dng tp hp
d liu c cha trong cc phn t ca
form chuyn t client n server bng
phng thc POST.
Ly d liu t cc phn t ca
form:
C php
VarName=
Request.Form[FieldName];

Lp Http Request

V d:
txtUser
txtPwd

Default.aspx

<form method="Post" action=CheckUser.aspx">


UserName: <input type="text" name="txtUser" /><br />
Password: <input type="password" name="txtPwd" /><br />
<input type="Submit" name="btnLogin" value="Login" /><br />
6
</form>

Lp Http Request
V d (tt):
Ti trang CheckUser.aspx, ly gi tr ca UserName
v Password trn form

<b>UserName:</b>
<%=Request.Form["txtUser"]%><br />
<b>Password</b>
<%=Request.Form["txtPwd"]%>

Lp Http Request

Cc thuc tnh ca form Collection:


Count: tr v s phn t ca form chuyn
t client n server
C php:
Numelement=Request.Form.Count
;

Lp Http Request

Keys[i]: tr v tn ca phn t ti v tr
th i, trong i c gi tr t 0 n n-1
vi n=Request.Form.Count
C php:
Varfield=Request.Form.Keys[i];

Lp Http Request
V d:
<% int i, num;
string varField, varName;
num = Request.Form.Count;
for (i = 0; i < num; i++)
{
varField = Request.Form.Keys[i];
varName = Request.Form[varField];
}
%>

10

Lp Http Request
2.

Tp hp QueryString
(QueryStringCollection)
Request.QueryString dng ly d liu
c gi t client n server bng phng
thc GET hoc truyn d liu c cha tag
lin kt <a> Hyperlink.
QueryString c nh ngha l chui nm
sau du ? trong chui URL (Uniform
Resource Locator) trn phn Address ca
trnh duyt.
11

Lp Http Request
C 3 cch truyn tham s v gi file x l
Cch 1 : dng action , tham s truyn qua
phn t ca form
<Form method=Get
action=FileName.aspx>
Cc phn t ca form
</Form>
12

Lp Http Request
Cch 2 : dng action , tham s ghi sau
du ?
<Form
<Form method=Get
method=Get or
or Post
Post
action=FileName.aspx?
action=FileName.aspx?
var1=value1&var2=value2&>
var1=value1&var2=value2&>
Cc
Cc phn
phn t
t ca
ca form
form
</Form>
</Form>

13

Lp Http Request
Cch 3 : dng chui link , tham s ghi sau
du ?
<a
<a href=FileName.aspx?
href=FileName.aspx?
var1=value1&var2=value2&>
var1=value1&var2=value2&>
Text
Text hoc
hoc image
image
</a>
</a>

Cc tham s c khai bo cch nhau bi


du &, gi tr ca mi tham s t sau du =
14

Lp Http Request

Nu tham s khng c gi tr th gi tr
tr v ca tham s khi s dng
Request.QueryString l NULL.

Vi 3 cch truyn d liu t Client nu


trn, pha Server s dng tp hp
QueryString nhn d liu

15

Lp Http Request

Ly d liu t cc phn t ca form


hay <a>:
C php:
Request.QueryString[varName
];

16

Lp Http Request
Cc thuc tnh ca
QueryStringCollection:
Count: tr v s phn t ca form chuyn
t client n server
C php:
Numelement=Request.QueryString.Cou
nt;

17

Lp Http Request

Keys[i]: tr v tn ca phn t ti v tr
th i, trong i c gi tr t 0 n n-1 vi
n=Request.QueryString.Count

C php:
Varfield=Request.QueryString.Keys
[i];
V d:
18

Lp Http Request
txtUser

txtPwd

<form method=Get" action=CheckUser.aspx">


UserName: <input type="text" name="txtUser" /><br /
Password: <input type="password" name="txtPwd" /><br />
<input type="Submit" name="btnLogin" value="Login" /><br />
</form>
19

Lp Http Request

Ti Server (file CheckUser.aspx), ly gi


tr ca UserName v Password

<% string strUser, strPws;


strUser =
Request.QueryString["txtUser"];
strPws =
Request.QueryString["txtPwd"];
%>
20

Lp Http Request

Hoc: Trong cc HyperLink

<a href=ReadNews.aspx?
typeNews=edu&NewsID=98708>
Scholaship of IBM
</a>

Ti ReadNews.aspx mun ly gi typeNews


and NewsID, s dng c php:
Var1=Request.QueryString[typeNews];//Var1=ed
u
Var2=Request.QueryString[NewsID];//Var2=9870
8
21

Lp Http Request
3.

Tp hp Cookies (CookiesCollection)
Cookies l mt mu thng tin nh dng lu
tr thng tin ca ngi dng trn my client
Cookies c 2 dng:
Cookies[varcookies].Value: tr v gi
tr cha bin cookies l varcookies (1 chiu)
Cookies[varcookies]
[Subvarcookies]: tr v gi tr cha bin
cookies l subvarcookies (nhiu chiu)
Cookies[varcookies]
[Subvarcookies][hhhhhh]:
22

Lp Http Request
Thuc tnh ca Cookies:

Count: Xc nh my client c bao nhiu


phn t cookies
C php:
Numelement=Request.Cookies.Cou
nt;

23

Lp Http Request

Keys[i]: tr v tn ca phn t ti v tr
th i, trong i c gi tr t 0 n n-1 vi
n=Request.Cookies.Count
C php:
Varfield=Request.Cookies.Keys[
i];

24

Lp Http Request
V d:
<% int i, num;
string varField, varName;
num = Request.Cookies.Count;
for (i = 0; i < num; i++)
{
varField = Request.Cookies.Keys[i];
varName = Request.Cookies.
[varField].Value;
}
%>

25

Lp Http Response
Http Response c s dng truyn
d liu webserver n webBrowser
Data

26

Lp Http Response
Cc thuc tnh

Buffer: c 2 gi tr true v false quy nh


cch truyn ti d liu:

True: Hon tt cng vic v gi mt ln

False: Thc hin xong n u th gi


n

27

Lp Http Response
C php:

Response.Buffer=true/false;

isClientConnected: Kim tra my client c


yu cu kt ni n server, kt qu tr v gi
tr true/false

Cookies: c s dng ghi Cookies.


Cookies c th hin trong asp.Net l lp
Httpcookies

28

Lp Http Response

Khi to bin cookies:

Httpcookies varck= new


Httpcookies(CkName);

Ghi cookie vo client


varck.Value =value;

29

Lp Http Response

Server ghi vo my trm 1 bin


cookies
Response.Cookies.Add(varck
);

Xa cookies
Response.Cookies.Clear()
;

30

Lp Http Response

V d:khi to bin cookie v gn gi tr

<% string v, v1,v2,v3;


HttpCookie ckInform = new HttpCookie("Infor");
ckInform[Us"] = "abc";
ckInform["Pwd"] = "xyz";
ckInform["Author"] = "123";
Response.Cookies.Add(ckInform); // server add
cookies vo my client
%>
31

Lp Http Response
// c Cookies
v = Request.Cookies[Infor].value ;
// result :v=abc,xyz,123
// hay dng
v1 = Request.Cookies[Infor] [Us];//abc
v2 = Request.Cookies[Infor] [Pwd];//xyz
v3 = Request.Cookies[Infor]
[Author];//123
32

Lp Http Response
Cc phng thc
Clear(): Xa vng tm
C php:
Response.Clear()

End(): Kt thc tin trnh x l trn


Server v y d liu ti Client
Flush(): Kt thc tin trnh, quay li thc
hin tip
33

Lp Http Response
V d:
<%
int i;
Response.Buffer = true;
for (i = 1; i < 10000; i++)
{%>
<%=i%>
<%if (i % 10000 == 0)
Response.Flush();%>
<%}%>

34

Lp Http Response

Redirect(url) : dng chuyn client


sang mt URL khc
Redirect(url, Boolean): dng chuyn
client sang mt URL khc, trong :
Nu tham s Boolean =true th khi thc
hin xong lnh Response s thc hin
tip
Nu s Boolean =false th khi thc hin
xong lnh Response s dng
35

Lp Http Response

V d:
Response.Redirect(T1.aspx);
m=m+1;
Try
{
Response.Redirect(T1.aspx, false);
m=m+1;
}
Catch
{
Response.Redirect(T2.aspx);
}

36

Lp Http Response

Write(Data): Ghi d liu ra trang web

<% = strname
hoc

%>

Response.Write(strname)

37

Lp Http Server
i tng Server cung cp cc phng thc
gip chuyn iu khin gia cc trang vi
nhau, ly thng tin v m li, encode,
Cc thuc tnh
MachineName: tn server
ScriptTimeout: thit lp thgian x l ti
a 1 file
V d:
Server.ScriptTimeout=100s //thit lp thgian
mc nh

38

Lp Http Server
Cc phng thc

MapPath(URL): nh x ng dn o
thnh ng dn vt l cho mt tp tin trn
Server

C php:

Server.MapPath(path)

Path l chui thc hin ng dn tng


i ca tp tin trn ng dng Web
Phng thc s tr v chui hin thc
ng dn vt l ca tp tin .
39

Lp Http Server

HtmlEncode(str): dng m ho HTML


thnh chui
C php:
Server.HTMLEncode(str)

40

Lp Http Server

Khi m ho chui HTML sang chui bnh


thng nu gp cc k t nh sau s
chuyn sang k t tng ng:

K t <chuyn thnh &lt;

K t >chuyn thnh &gt;

K t & chuyn thnh &amp;

Du i chuyn thnh &quot;


41

Lp Http Server
HtmlDecode(strcontent):gii m cc th
HTML
V d:
Str=n&gt;5
Str=server.HtmlDecode(str); // kt qu
n>5

42

Lp Http Server

Transfer: gi tt c thng tin m n


x l t trang ASP hin hnh sang trang
ASP khc
C php: Server.Transfer(path)

Trong , tham s path l ng dn ca


tp tin ASP cn chuyn iu khin.
43

Lp Http Server

Phng thc URLEncode: Tng t nh


phng thc HTMLEncode, phng thc
URLEcode dng m ho URL thnh
chui
C php
Server.URLEncode(str)

44

Lp Http Server

Khi m ho chui URL sang chui bnh


thng nu gp cc k t nh sau, s
chuyn sang k t tng ng:
K t khong trng chuyn thnh du
+
K t khng thuc k t ch v s s
chuyn thnh s hexadecimal.

45

Lp Http Server

Phng thc CreateObject: Phng


thc ny (khng c gi tr tr v) dng
to i tng thnh phn trn Server

C php
Server.CreateObject(progid)

Tham s progid ch nh loi i tng


cn to vi nh dng
[Vendor.] Component[.Version]
46

Lp Http Server
V d:
To i tng ADODB.Connection, ADODB.Recordset,
MSWC.AdRotator, ...
<%
Set myAd=Server.CreateObject(MSWC.AdRotator)
Set myCon=Server.CreateObject(ADODB.Connection)
Set myCon=Server.CreateObject(ADODB.Recordset)
%>`
47

Lp Http Server

Phng thc Execute: khng c gi tr


tr v, dng gi mt tp tin .asp v x
l n nh mt phn ca kch bn ASP

C php
Server.Execute(path)

Path l ng dn v tn tp tin asp cn


thc thi, tng t nh gi th tc trong
nhiu ngn ng lp trnh khc.
48

Session

i tng Session l mt websever c


bn s dng trong ASP v ASP.NET c
dng lu tr trng thi.

Thng tin c lu tr trong Session l ca


mt ngi dng trong mt phin lm vic c
th.

Web Server s t ng to mt i tng


Session cho mi ngi dng mi kt ni vo
ng dng v t ng hy chng nu phin
lm vic kt thc
49

Session

Khi to:

Session[SessionName]=va
lue
Ly gi tr t bin Session

varName=Convert.ToString(Session[SessionN
ame])
Session[SessionName]=Session[SessionNa
me] +value + @
@: dng tch cc gi tr (c th chn ty )
50

Session
Thuc tnh:
Timeout: Qui nh khong thi gian (tnh
bng pht) Web Server duy tr Session nu
ngi dng khng gi yu cu no v li
Server.
Gi tr mc nh ca Timeout l 20. Nu
khng c yu cu no k t ln yu cu sau
cng mt khong thi gian l <Timeout>
pht, i tng Session m Web server cp
cho ln lm vic s t ng c gii
phng.

51

Session
Nhng yu cu sau , web server coi nh
l mt ngi dng mi, v s cp mt i
tng Session mi
V d:
Session.TimeOut = 100 //Thit lp thi gian
cho 1 phin lm vic ca user l 100 pht
C th thit lp thuc tnh ny trong tp tin
Web.config trong <system.web>
<sessionState timeout=100" />

52

Session

SessionID: cha ID ca session ang


kch hot, mi user c phn bit bi
sessionID gi l m phin lm vic.
Count: tr v s session trong mt ng
dng

53

Session
Phng thc
Abandon(): gii phng vng nh c
dng duy tr i tng Session trn
Web Server ngay khi c gi thc hin.
Nhng yu cu sau c Web server
coi nh l mt ngi dng mi.
Remove(SessionName): xo d liu
trn bin VarSession
RemoveAll(): Xo d liu, nhng
sessionID vn tn ti

54

Session
V d: To chc nng ng nhp v thot
T Menu chn Website Add new item
Web User Control, t tn l Login.aspx
Thit k giao din nh sau:

55

Session
Khi to v Hu thng tin ng nhp ca ngi dng
trong Session
void Session_Start(object sender, EventArgs e)
{
// Tng s khch ving khi c 1 phin lm vic ca user
Application["SoKhachVieng"] =
(int)Application["SoKhachVieng"] + 1;
// Thit lp thi gian TimeOut cho mi phin l 30 pht
Session.Timeout = 30;
// t trng thi ban u ca user l cha ng nhp
Session["LOGIN_OK"] = false;
}
56

protected void btnLogin_Click(object sender, EventArgs e)


{
if (Authority(txtUserName.Text, txtPassword.Text))
{
// Lu thng tin ng nhp thnh cng vo Session
Session["LOGIN_OK"] = true;
// n chc nng Login
panelLogin.Visible = false;
// Hin th chc nng logout v li cho mng
LblGreeting.Text = "Cho mng bn " +
txtUserName.Text
+ "
ng nhp
website!";
}
else
{
// Lu thng tin ng nhp tht bi vo Session
Session["LOGIN_OK"] = false;
57
}

Session
protected bool Authority(string user, string
pwd)
{
if (user == "aaa" && pwd == "1234")
{
return true;
}
return false;
}
58

Application

i tng Application c s dng


qun l tt c cc thng tin ca mt ng dng
web, bao gm cc file, trang web, s kin,
module v code trong th mc web o(virtual
directory) v cc th mc con ca n

Application chia s thng, ngha l mt


Application sau khi khi to th mi ngi s
dng c th truy cp i tng ny
59

Application

S kin: global.asax
Application_Start: Khi ng dng web
ln u tin c gi
Application_End: Khi ng dng web
kt thc.

60

Application

Khi to:
i tng Application c to khi client
yu cu bt k trang no trong ng dng
Application[TenBien] = Value ;

61

Application

Ly gi tr:
string s = (string)
Application[chuoi] ;
int count = (int)
Application[count] ;

62

Application
V d: Khi to v lu thng tin S ngi
duyt website khi website c kch hot ln
u tin
void Application_Start(object sender,
EventArgs e)
{
Application["SoKhachVieng"] = 0;
// Khi to s khch ving ban u l 0
}
63

Application

M trang Global.asax ch View Code

void Session_Start(object sender, EventArgs e)


{
Application["SoKhachVieng"] =
int)Application["SoKhachVieng"]+1;
// Tng s khch ving khi c 1 phin lm vic ca user
}

64

Application
V d :Ly thng tin s khch ving website
v hin th ra mn hnh
M trang Index.aspx ch View Code, X
l trong hm Page_Load nh sau:
protected void Page_Load(object sender,
EventArgs e)
{
lbtCount.Text = "S khch ving thm :" +
Application["SoKhachVieng"].ToString();
}

65

You might also like