)
Len CAM ON
Tnrac tien, em xin pile!) duqc giri lei cam an du sac tai Thac si" Tran Quang Duy,
ngtred they di nhiet tinh gulp di, va hiring din em tong qui trinh thvc hi8n khea luan nay.
Em ming xin giri den qu9 thiy c8 dang giang day tat truing Dai hoc Thing Long
n6i chung va cac thiy c8 trong khoa Toan Tin n6i ring lei biet an chin thanh, cam an
they c8 vi di het ling truyen dat cho em nhimg kien thirc trong nhing nim thing ngai
tren ghe nha tnrImg. Kinh chic Glut thiy co Wen lu
108 trang |
Chia sẻ: huong20 | Ngày: 07/01/2022 | Lượt xem: 465 | Lượt tải: 0
Tóm tắt tài liệu Khóa luận Tìm hiểu về công nghệ asp.net MVC framework của microsoft, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
au math khoe, gat hai duqc nhie'u
thanh cong trong sv nghiep.
Dii di cif) gang het sire de dux hi4n de tai nhung kh6 tranh kh6i nhung thieu sot,
em rat mong titian dugc sir gap 9 cis qu9 thiy co, cac anh chi va cac ban a em co the
tier b0 hen.
Ha noi, ngizy 16 thtbtg 6 nom 2013
Sinh via thw hien
Deu Huy Hang
Trang 2/109
M1JC LI,IC
DANH MIX CAC THUAT NGU VA T& VIET TAT 6
DANH MIX HINT ANH 7
GIOI THItU CHUNG
9
CHUONG 1 - TONG QUAN VE ASP.NET MVC FRAMEWORK 11
1.1. Gidi thieu ye me hinh MVC 11
1.1.1. Cie thanh plan cilia MVC
11
1.1.2. Hoat dOng coa m6 hinh MVC 12
1.1.3. LIU diem yit nhuot diem cda MVC 13
1.2. Gbh thieu ye ASP.NET 13
1.3. ASP.NET WebForm truyen thong
13
1.4. ASP.NET MVC Framework
15
1.4.1. Nguen g6c 15
1.4.2. Cac thanh Oat ca ben cat ASP.NET MVC
16
1.4.3. Qua trinh host dOng cna mOt img dung ASP.NET
MVC 17
1.4.4. MOt s6 tinh rang n6i 1* cna ASP.NET MVC Framework 17
1.4.5. Uu diem 18
1.4.6. Nhugc diem 18
1.4.7. So sinh gilla ASP.NET
webform vit ASP.NET MVC 18
1.4.8. Lan quen yeti ung dung ASP.NET
MVC Framework 20
CHUONG 2 - CAC THANH PHAN CHINH CUA ASP.NET MVC 34
2.1. Controller yi action method 34
2.1.1. Cac ldp chinh elm Controller 34
2.1.2. Action Method 35
2.1.3. Clic ket qua tni ye kik ActionResult
36
2.1.4. NhAn cac ditu vito
37
2.1.5. Filter 40
2.2. View
45
Trang 3/109
2.2.1. Khasi niem ViewEngine
45
2.2.2. Geri thieu ye Razor 45
2.2.3. Them nOi dung dOng cho Razor View
46
2.3. Model 62
2.3.1. Model Binding 63
2.3.2. Model Validation 69
2.4. URL WI routing 75
2.5. jQuery trong ASP.NET
MVC 79
2.5.1. Gied thieu 79
2.5.2. Co ban ve jQuery 80
2.5.3. jQuery tich hqp trong ASP.NET
MVC 80
2.6. Bio mat trong ASP.NET MVC 81
2.6.1. Cross-Site Scripting 81
2.6.2. SQL Injection
82
CHUONG 3 - CHUONG TRINH uNG DIJNG
85
3.1. MO to bai town
85
3.1.1. Cac dti ttrqng tham gia vio he thong
85
3.1.2. Cac chirc thing chinh ctia img dung
85
3.1.3. Su dO use-case chfrc &mg caa he thong
86
3.2. TO chirc ca soft der lieu
88
3.2.1. M8 hinh cc se de lieu
88
3.2.2. Chi tiet cac bang trong ca so de lieu
89
3.3. Mai trirtmg cal dat
91
3.4. MA ngutn chuong trinh
92
3.4.1. Cau frac cac file trong ting dung
92
3.4.2. Anh za cac lop yen file twang img
93
3.4.3. So luqc ve cac lop chinh
96
3.5. MOt so hinh anh minh hqa ve ting dung
105
Trang 4/109
KET LUAN 108
TAI LIIrU TRAM KHAO 109
Trang 5/109
DANH MVC CAC THUAT NG& VA TIT VIT TAT
Class
Request
Action method
Client
Server
Code
NameSpace
Browser
Framework
Filter
Validation
MVC
CSDL
HTML
HTTP
Lop
You au
Phuong thirc hog Ong
May lchich
May chit
MA lenh
Kh6ng gian ten
Trinh duy6t
Nen tang
BO 19c
Xic nhin
Model View Controller
Co so dii li6u
HyperText Markup Language
HyperText Transfer Protocol
XSS Cross Site Scripting
URL Uniform Resource Locator
Trang 6/109
DANH MDC HINH ANH
Hinh 1.1 Ma hinh MVC 11
Hinh 1.2 Quy trinh hogt dung ctia mo hinh MVC 12
Hinh 1.3 Cong nghe ASP.NET
WebFortn
13
Hinh 1.4 Cie thanh phdn co ban &la ASP.NET
MVC 16
Hinh 1.5 ASP.NET
WebForm vas ASP.NET MVC 18
Hinh 1.6 Khasi tgo Ong dyng ASP.NET MVC 20
Hinh 1.7 HOp thogi New ASP.NET
MVC3 21
Hinh 1.8 Minh hpa mot Solution Explorer ctia ASP.1VET MVC3
22
Hinh 1.9 Giao Bien co ban khi chgy Ong thing ASP.NET
MVC ca Ian 23
Hinh 1.10 Giao dien thy chink thay dal churmg trinh trong khi Debug
24
Binh 1.11 Kit not cash die' lieu bang Entity Framework — boot 1
24
Hinh 1.12 Kit nil ca set die lieu bang Entity Framework — bleat 2
25
Plink 1.13 Kit not co sb &I lieu bang Entity Framework — bulk 3 25
Hinh 1.14 Kit not co sas dir lieu bang Entity Framework — burry 4
26
Hinh 1.15 Kit nii co set die lieu bang Entity Framework — burry 5
27
Hinh 1.16 Kit co so die lieu bang Entity Framework— buoy 6
27
Rink 1.17 Minh km them mai met Controller -1
28
Hinh 1.18 Minh hoe them mai mot Controller - 2
28
Hinh 1.19 Minh hoe them mai mot Controller - 3
29
Hinh 1.20 Minh hpa them mai mot Controller - 4
30
Hinh 1.21 Minh hpa them mai mot View -1
31
Hinh 1.22 Minh km them mai mat View - 2
31
Hinh 1.23 Minh hpa them mbi mot View - 3
31
Hinh 1.24 Minh hpa them mai mot View - 4
32
Hinh 1.25 Minh Iota them mot mot Model
33
Hinh 2.1 Minh hpa tgo mat lnline HTML helper
48
Hinh 2.2 Kit qua sei dyng Section
58
Trang 7/109
Hinh 2.3 Tao mat Partial View 60
Binh 2.4 Minh hpa ket qua tra ve mat Child Action 62
Hinh 3.1 Sa do use-case char tiling cue admin 86
Hinh 3.2 Sa da use-case char ming ctia ngvoi dung 87
Binh 3.3 MO hinh ca so du lieu cho Ling dung 88
Binh 3.4 Trang chti czia website
105
Hinh 3.5 Giao then them min nha xucit ban 106
Hinh 16 Danh such cut nha nazi ban trong he thong 106
Hinh 3.7 Giao dien chinh situ thong tin ve nha xuat ban 107
Hinh 3.8 Giao dien chat ming cAlt hang 107
Trang 8/109
GICTI THIOU CHUNG
Tnr6c day khi thiet ke min website, nhang ngtrai mai tim hieu s8 phai ton rat nhieu
ding sac de hoc, nghien dm, vi hp se mat mot khoing thin gian kh6ng rthe de twin thinh
website yen day dit cac tinh ding. Sau da, ho can bo ra rat nhieu cong sac chay chit
cling nhu sira 18i, khic phuc cac van de bao mot_ va cu6i cuing sin pham hp lam ra cling
chua chic "sfinh vai" 81:cc v6i cac sin phial ra dfri truerc do. Nam bat duqc dieu nay,
Microsoft da cung cap mot nen tang phat then ling dung web nham ginp ich cho ngulyi sir
dung, do lit ASP.NET.
ASP.NET MVC la met trong taming mo hinh duqc cung cap beri ASP.NET . NO duqc
xay dung theo m8 hinh MVC (Models, Views va Controllers), gulp Lich biet cac thinh
phan trong ling dung. Val me hinh nay, nhang lap trinh vien co the a ding xay dung
nhang website von cAu truc chat che, rinh mach. Ngoai ra, no cling giop cho viec sira 18i,
thay the vi thin nghiem cac chic nang ctia website tra nen a ding horn.
Veri nhiing tru diem tren, ASP.NET
MVC gin rthir se tra thanh met nen tang chinh
trong viec xay dung va phat trien cac website. Vi vay, trong lchoa luan Mt nghiep nay, em
se tim hieu ve cong nghe ASP.NET
MVC Framework caa Microsoft va xay dung melt
ling dung dua tren ding nghe nay.
Myc dich nghien ct?u tali kh6a ken:
- Tim Mau ve met cong nghe mai;
- Phuc vu cho viec xay dung mot website giai thieu vi dat hang sach, nhim lip dung
nhftng kien tit da duqc tim hieu vao thuc te;
- Clung cap met tai lieu Tieng Viet ve cong nghe nay.
Phtrong phip nghien coy:
Nghien cuu 15, thuyet th8ng qua ebook dui Microsoft va the then din;
- Tang buck tim hieu cac vi du ca ban, sau do lip dung vao met ling dung nho, din din
phat trien thinh met chuang trinh;
- Ring hop cac kien thac thinh bio cao va xay dung chuang trinh Ling dung.
Trang 9/109
Nei dung dm tai lieu duce chia lam 3 chtrang.
Chuang 1 — Tong quan ve ASP.NET MVC FRAMEWORK
Chuang nay se gioi thieu ve m6 hinh MVC cling nhu dtra ra cai nhin tong quat ve
ASP.NET MVC.
Chuang 2 — CIIc thenh phin chinh cua ASP.NET
MVC
Chucmg nay se tim hieu chi tiet ve ck thinh phan chinh cita ASP.NET
MVC, nhu la
Controller, Model, View, URL, jQuery va met so van de bao mat tong ASP.NET
MVC.
Chuang 3 — Chuang trinh inng dung
Nei dung chuong nay la giai thieu ve trig dung duce xay dung dua tren cling nghe
ASP.NET
MVC. U'ng citmg de la met website cita dra hang sach yea Unit nang quan 19,
giai thieu san phim va (tat hang truc tuy'en.
Trang 10/109
Chuang 1- Tong quan ye ASP.NET
MVC FRAMEWORK
CHIMING 1 - TONG QUAN V ASP.NET
MVC FRAMEWORK
1.1. Gitri thi$u ye m8 hinh MVC
Khi cic phin mem img dung ngay cang phit then km mph vi da clang thi vin de thiit
lei hen tnic cho phin mem cling tr6 n6n phiic tap hon. Nhung ung dung web gig day
cling da phit trien din met tam cao mei, ching kh8ng can la nhOng (mg dung web thing
thirang yeti chirc nang hiin thj thong tin ma la nherng hi dicing thong tin he trq nhung
trang web tucmg tic yeti ngubi ding. Nhieu giai phap thiet lea kien frac hi thong duqc dua
ra vat aux dich lam cho hi tilting linh hog, mem deo, de phit then vi ton to lau dii.
Trong de, giai phip this& ke theo m8 hinh kien true MVC duqc Trygve Reenskaug de xuAt
vao nhOng am 70 coa the ky 20 to ra rat hieu qua.
Mo hinh MVC (Model - View - Controller) la met kien trite phin mem hay mo hinh
thiitke dirge sir dung trong ky thuat phtm mem. N6 glop cho cic nha phit then tech img
dung cila hq ra thanh 3 pill lchic nhau Model, View va Controller. mOi thanh phin c6
met nhiim vu ring biit va dije lap vei cic thanh phin khic.
1.1.1. Cac thenh phin ctia MVC
1111
Controller
Hinh 1.1 Mii hinh MVC
Model
Day la thanh phial chira tit ca cic nghiip vu logic, phuang thee zit 1Y, try mit
database, did tugng mo to de liiu nhu cic Class, ham xfr 19...
Trang 11/109
I
Chuang 1- T6ng quan ye ASP.NET
MVC FRAMEWORK
View
Dam nhan viec hien thi thong tin, twang tac veri ngueri dung.
Controller
Gift nhiem vu nhOn (lieu huong cac you clu tit ngubi dimg va goi ding nhftng phuang
thfrc xir ly chung.
1.1.2. Hoot dung cart ma hinh MVC
Day la mist each dan giin de m8 to lei luting su kien duce xit ljr trong MVC:
- User twang tic yeti View, bang each click vim button, user gui yeti cku di.
- Controller nhOn va dieu huong chimg den dung phuang thirc zit l5r er Model.
- Model nhan thong tin vi thvc thi cac you au.
- 'Chi Model hoan tat viec zit ljr, View se nhon &I lieu tir Model vi Controller se phin hhi
lai ngtroi dung.
I Y ear I
n11.- User gesture
•
Invoice action
Notify changes
Ask for changes
11.1 Butane the
requested task •
Updated Model
New View served
to ithe user
4—
hinh 1.2 Quy trinh hoist dfing cua mo hinh MVC
Trang 12/109
Chtrcmg 1- Togng quan ye ASP.NET
MVC FRAMEWORK
1.1.3. U'u diem va niurqc diem ala MVC
U'u diem:
The hien tinh chuyen nghiep trong lap trinh, phan tich thiet ke. Do avec chia thanh cac
thanh phith (Ric lap nen ginp phat trien img dung nhanh, don gian, de nang cap, bao tri..
v Nhuot diem:
Din vei du an nheo viec ap dung m8 hinh MVC gay ding kenh, tin then gian tong qua
trinh phat trien. TAn then gian trung chuyen d0 lieu cha cac thanh phin.
1.2. Giafri thieu ye ASP.NET
ASP.NET
Ia mOt nen tang phat trien (mg dung web (web application framework) &roc
phat trien va cung cap ben Microsoft, cho phep nherng ngutii lap trinh to ra nhitng hang web
ding, nhOng img dung web va nhOng dich vu web. Lin diu tien &roc dim ra thj throng vito
thang 2 nAm 2002 cimg v6i phien ban 1.0 ciut .NET framework, la ding nghe nen tiep ctia
Microsoft Active Server Pages(ASP). ASP.NET
ducc bien dich duel clang Common
Language Runtime (CLR), cho phIp nhang win lop trinh vitt ma ASP.NET
yeti bit kk
ngon ng0 nao duqc ho try bei .NET language.
1.3. ASP.NET
WebForm truyen thong
ASP.NET
la mOt buec nhay vot khi lin diu )(Wit hien, nhim thu hop khoang cach gifta
pith trien Window Form Wong di ft:mg (cd thing thai) va phat trien web hueng HTML
(Ichting tang thai) . Hinh duel day munh boa cong nghe ASP.NET
Webform
ASP.NETWeb FOMIII
Ante/IS components (pps, buttamone.) plus
suptsful, obisebodned all programming modsi
ASPNET
A int to hoctitT spolleations In KS Mcnnoles webers
product), Clog you Interact MO IITTINataises and taponses
Att.
A muttiblipearwippdadepbtform
(kaftan* thetbas-4, biasdikt Ns own 0.4)
Hinh 1.3 Cling nghg ASP.NET
WebForm
Trang 13/109
Chuang 1- Thng quan ASP.NET
MVC FRAMEWORK
V6i Web Forms, Microsoft da c6 in di HTTP vi HTML bang cach me phong giao dien
nguiri ding nhtr la met doi mow dieu lchien (Control) ce cau tnic hoot deng phia Server.
Mill di Wong dieu khien (Control) theo doi tang thai cim no thong qua cac you au (sir
dung tinh ring ViewState), bieu dien bang cric ma HTML !chi can thief. Ngoai ra, no con to
Ong kit not cac so kien phia Client (nhu so kien bem button) ten cac so kien phut Server
duqc xu ly ttrcrng img. Thoc te, Web Forms 11 met 16p trim tucmg khOng to duqc thiet ke de
cung alp giao clien ce xir ly so kien thong qua Web.
v NhOng diem Imut chi cast ASP.NET
Web Forms
ASP.NET
Web Forms truyin thing tong la met y mow rat tit, nhtrng thou te lei phit
sinh nhOng dieu phim tip khic. Theo thin gian, viec sir dung ASP.NET
Web Forms
da nou WI len met so thieu set;
- ViewState: Co the duy tri trang thiti thong qua cac yeti ta u (request) thutmg mang lui
ket qua la nheng IchOi luqng 16n da lieu duqc chuyen giao giOa client vi server. Da
lieu nay c6 the len den hang tram Kbs b met img dung web vira phai, vi n6 di qua lei
v6i bit ky yeu cau nito. Dieu nay lam ch..* di died gian ch6 /bin hoi vi lam ding nhu
cau bang thong dm server;
Page life cycle (Chu k9 ton tai ciut met trang web): Lit co the ket not sjr kien phia
client veil mA xir 19 so kien phia server. NO phfrc tap vi a bj pha ver;
thirc sai ve viec tech biet cac thinh phen: M8 hinh code-behind cita ASP.NET
cung
cap met gisi phip cho phep img dung due code ra khei cac clang HTML vio thanh
met 16p code-behind net:1g biet. Dieu nay duqc hoan nghenh reng rai yeti viec tech biet
giao dien veri mA )(Cr ly. Nlumg thou te, nhOng nha phat trier duqc khuyen khich pha
ten mA giao dien veri mA )(Cr 11 img dung (nhu la xir ly CSDL) tong nhfmg 16p code-
behind khOng 16. Neu khong c6 so tech biet gift cac thinh phin, ket qua cuoi cling
c6 the nit kh6 hieu;
Limited control over HTML (gidd Mtn kiem spat HTML): Server control to to ra cat
mA HTML, nhtmg khang phai la mA HTML ma to mong dqi. Ngoii viec mA HTML
min chfing thutmg khong than theo lieu chuin web hoes khong sir dung tot CSS ma he
Trang 14/109
Chuang 1- T6ng quan ye ASP.NET
MVC FRAMEWORK
thong cac server control can tao ra cat gia tri ID phirc tap vi khong down truer duqc,
lim kho khan trong viec sir dung JavaScript;
KM ming lciem thir thap: Cac nhit thiet ke caa ASP.NET
khOng the du down ring Idem
thir to gang se tra thank met phan tat yeu caa xu htrong phat then phan mem. Kh8ng
ngac nhien khi kien tnic chat the ma ho thiet ke la 'thong pha hqp vai viec tich hqp
Ici'em thir.
ASP.NET
van tip tuc phat trien. Phien bin 2.0 them mat thanh phan chuan de giiun bat
se luqng code ma chimg to can phiti to viet. Vai phien ban gan nhit ASP.NET
4.5, mui thir
da duqc ming cep &mg ke. Phien ban ASP.NET
4.5 di ap dung met se tinh ming dm
ASP.NET
MVC vie Web Forms. Tuy vay, van can met han the ten to ben trong no.
1.4. ASP.NET MVC Framework
Nhu da giai thieu et Olin truck, ma hinh MVC vai uu diem cam minh di duce img dung
trong met se alc framework. Trong do pluii ke den ASP.NET
MVC, met framework ma
nguen ma mai, xay dung tren ASP.NET.
ASP.NET MVC Framework duqc thvc thi tree me hinh model—view--controller (MVC).
NO gun nhg, de Bang kiem thir phan giao dien, tich hqp cat tinh ming cira ASP.NET
. MVC
Framework duqc djnh nghTa trong namespace System.Web.Mvc va la met Olt cim Ichong
gian ten System.Web.
1.4.1. NguBn goc
Tuy ding nghe ASP.NET
MVC mai duqc ph& trien nhung no co ljch sir lau deri. Nen
tang MVC duqc phat minh bai Trygve Renskaug, Ong di viet tang giay dau den not ve
MVC vio nem 1978. Ban du no duqc gui la Thing Model View Editor pattern, nhung sau
de no ductc dot ten i i li Model View Controller pattern.
ASP.NET MVC lan du lien St Men d6 la trong du an me ngtdin ma MonoRail.
Nguen gac that sir de cho ra deri ding nghe Microsoft ASP.NET
MVC li do Scott Guthrie
(met trong nhOng nha sang tao m ASP.NET ) tren chuyen bay den Austin, Texas de not ve hOi
thao Alt.NET vao thing 10-2007.
Phien bin ASP.NET
MVC 1.0 duqc m dbi vio nam 2009.
Trang 15/109
Chuang 1- Tong quan ye ASP.NET
MVC FRAMEWORK
1.4.2. Cic think phan cabin ctia ASP.NET
MVC
ASP.NET MVC Framework
Binh 1.4 Ceic thank phan ca ban ciia ASP.NET
MVC
Models: Cac
tirgng Models li mOt phan ciia ling dung, cac di tucmg nay thiet 1#p
logic cila pit dO lieu cna ling dung. Meng thuimg, tic
tugng model lay vi luu tang
thai cita model trong CSDL. Vi dp nhu, mOt doi Meng Product (sin pan) se lay MI lieu tit
CSDL, than tic tree dit lieu va se c#p nh#t de lieu ter lin vao bang Products 6 SQL Server.
Views: Views la cac thinh phan ding di hien thj giao dien ngubi dung (UI). Thong
thubng, view duet tio dun vio thong tin dfr lieu Models. Vi dp nhu, view dung di c#3 nh#t
bang Products se hien thj cac hQp van ban, drop-down list, vi cac check box data tren trpg
thai hien nu cua mOt di Wong Product.
Controllers: Controller la cac thanh phan dung de quan ly Luang the ngubi dung, lam
viec vei model vi chip view di hien thj giao dien ngtrei ding. Trong mOt Ong dung MVC,
view chi dirgc dung cie hien thj thong tin, controller chju trach nhiem quan ly va dap tra nOi
dung do ngtrbi dimg nhop vi tuang tac vai ngubi climg. Vi du, controller se quan ly cac clu
lieu ngubi dung girl len (query-string values) vi gui cac gia trj de den model, model se lay chl
lieu tit CSDL nhe viz cac gia tri nay.
MO hinh MVC glop Chung to duet cac Ong dung ma chting phan tack rich red cat
khia earth c»a Ong dung (logic ye nh#p lieu, logic xu lY tic vu va logic ye giao dien). MO hinh
MVC chi ra min loni logic ke teen nen duet thiep li) 6 dau teen Ong dung. Logic giao dien
(UI logic) thuOc ve views. Logic nhtep lieu (input logic) thuoc ve controller. Va logic the vp
(Business logic — la logic xir ly th8ng tin, muc dick chinh cua ung dung) thuOc ve model. Sir
phan chia nay giim chang to giim bat dugc sir phirc tip cita Ong dung va chi top twig vac
Trang 16/109
Chuang 1- Tag quan ye ASP.NET
MVC FRAMEWORK
mOi khia canh can duce cai d#t a m8i that diem. Vi du nhtz chting to chi can top trung vito
giao Bien (views) ma khong phai quan tam den logic xer 1S, thong tin elm ling dung.
Su phan tach rach reit ba thanh phan cita img dung MVC can gitip cho viec 1#p trinh
dies ra song song. Vi du nhu mQt 1#p trinh vien lam tee vei view, 1#p trinh vien thti hai
lo cii dot logic ciut controller va lop trinh vien thir ba c6 the top trung vio logic the vu cia
model tai cling met thai diem.
1.4.3. Qua trinh host dung ctia mOt (rug Mpg ASP.NET
MVC
Cac giai doan thuc hien dui met ling dung ASP.NET
MVC duce trinh bay duei day:
NhiIn you eau dau den: Trong file Global.asax, cac doi tuqng Route duce them Arlo
RouteTable;
- Thuc hien viec djnh tuyen: Module UrlRoutingTable sir dung di tuqng Route dau
tien thich hqp trong top RouteTable de tao
tuqng RouteData. DOi ttrong nay sau
do lai tao ra doi tuqng RequestContext (ifittpContext);
- To ra trinh xir ly yeu eau ling dung: Doi tuqng MvcRouteHandler tao ra met the
hien ctia 16p MvcHandler va truyen vao no the hien RequestContext;
To ra controller. Cie dOi tuqng MvcHandler sir dung the hien cita RequestContext
de Lie djnh dig tuqng IcontrollerFactory qua d6 tao ra the hien controller;
- Thur hien vai tr6 cis controller: The hien MvcHandler gui phuong thirc Execute
cita controller;
- Goi action method: DOi tuqng ControllerActionInvoker se xac djnh action method
cur met 16p controller de ggi ra.
1.4.4. MOt so tfnh nang ndl bOt cem ASP.NET
MVC Framework
Tach bath cic tic vu cita ling dung.
- C6 kha nang ma rQng (extensible) & kha Hong nhimg (pluggable). Cie thanh phan ctia
ASP.NET
MVC duqc thiet ke de chting co the duce thay the hoc thy chink met cach
a ding.
- ASP.NET
MVC c6 thanh phan inh xa URL manh me, cho ph6p cluing to may dung
nhang ling dung c6 cac dia chi URL xtic tich va de tim kiem. Cac dja chi URL IchOng
Trang 17/109
Chuang 1- Tong quan ye ASP.NET
MVC FRAMEWORK
can phai c6 pan mer rang cita ten tap tin va duqc thiet ki de hO trq cac mau djnh clang
ten phu hqp vei viec toi uu hoa tim kiem (URL).
Hg trq cac tinh 'tang c6 sin ctia ASP.NET
nhu ca the zee thus ngteri dimg, quan
thenh vien, quyen, output caching va data caching, seession va profile, quan ly tinh
tang img dung, he thong cau hinh...
- ASP.NET
MVC 3 con be sung met view engine mei la Razor View Engine cho phep
thiet lap cac view nhanh cheng, de ding va ton it ding sec han so vei vies sir dung
Web Forms View Engine.
1.4.3. U'u diem
- De ding quan ly su phirc tap cua img dung bang cacti chic img dung thank ba thinh
phan model, view, controller.
- HO trq tot han cho mo hinh phit then ung dung butt:1g Itiem thin.
- He thOng djnh tuyen mash me.
HO trq tot cho cac img dung duct ay dung bei nhfrng dill co nhieu lip trinh vien vi
thiet ke ma yin quan ly dime tinh &mg dui img dung.
- HO trq nhfing tinh nang tat nhat de c6 6 ASP.NET .
1.4.6. Niurcrc diem
- Kh8ng Wong den su lden lam cho cac nhi phat trien ASP.NET
webfonn ldt6 khan.
- Yeu cAu c6 hieu biet ve HTTP, HTML, CSS va JavaScript.
1.4.7. So sal* gigra ASP.NET
webform vi ASP.NET MVC
Minh 1.5 ASP.NET
WebForm va ASP.NET
MVC
Trang 18/109
Chuang 1- Tong quan vg ASP.NET
MVC FRAMEWORK
.Net Framework: 118 trq clic loo img dung nhu desktop, web va cac img dung console.
ASP.NET
framework la met phin cha .NET framework, duct xay dung nhirn 118 trq
cac Ung dung Web (caching, authentication va authorization).
Trong di, Microsoft co 2 luremg phat trier web ten nen ASP.NET la ASP.NET
Webforms va ASP.NET
MVC. Muc dich ra dbi cita ASP.NET
MVC chi la su thay dei ye
mit to duy, no khetng hoan town thay the cho cO img dung ASP.NET
Web Forms, va viec
chcm huing nao de &Ay dung met website la thy thuk vao cac nha phat trier.
Su khac bier gifta 2 Wong phat trien nay dirge the hien dual bang sau.
Tinh ring ASP.NET ASP.NET MVC
ICien tnic chuong trinh Kien frac me hinh
WebFotm > Business >
Database
Kien tnic sir dung viec phin chic
chtrong trinh thanh Controllers,
Models, Views
Cu phap chuang trinh Sir
dung cu phip cita
webrom, eat cac su kien va
controls do server quan 19
Cac str kien duqc dieu khitn b61
controllers, cac controls khong do
server quan 19
Troy cip du lieu Sir dung hiu het cac tong
nghe fly cip de lieu trong
Ung dung
Phan lin dung LINQ to SQL class
de tao me hinh tray cep dei urging
Debug Debug chuong trinh phii
thuc hien tit ca bao gom the
lap truy 4p de lieu, sum hien
thj, dieu lchien cac controls/
Debug c6 the sti dung cac unit test
kiem tra cac phircmg thirc trong
controller
Ttic de phan tai TOG de chin MU trong
trang co qui Situ cac
controls vl ViewState qui
lon
Phan tai nhanh han
Tuang tic veri
javascript
KM Idt6 IchAn Thu*1 19i
Trang 19/109
Chuang 1- Tong quan ye ASP.NET MVC FRAMEWORK
URL Address au tine dia chi URL c6
dung
.aspx?&
Cau tnic dia chi Sob much theo
dung Controllers/Action/ID
1.4.8. Lim quen v61 (mg Eking ASP.NET
MVC Framework
IM61 tgo mOt Gig dpiw MVC
Trong bO pan mainVisual Studio 2010 &I co sin ASP.NET
MVC Framework, to tao
mOt du ga mbi then dubng din sau: File > New Project > Visual C# > Web > ASP.NET
MVC Web Application.
Now porn 'a
Slid TOWN=
Puffin Inteffionce
Kul lac
• Vowel Ca
Madam
Office
Cloud
Pffilaffia9
ShrePoit
Sivoight
Tel
VICF
Waal'
Visual C••
Weal Co
a A9WWe yofficalion
a 19/ET INC 2 Web Appia♦es
a AMC MI/CiffiliAppla...
a ASPIC INC. Mr Appicsbee
WIC Empty WS Application
a WIC WC 2 brae We Sopfeseen
ASPMET Drente Dab as Web Anita
ASPIC DynelicDolo Jails 911. Wayfratan
Typo Veal Ca
A reject Ice aseffiej as appraise Sig
ASPIC WIC 3
was Ca
red Ca
VINSON
Vaud Ca
Veal Oa
was cc
Keel OP
was Cl
Hinh 1.6 Khoi too sing Ming ASP.NET
MVC
Trang 20/109
Chuang 1- T6ng quan ve ASP.NET
MVC FRAMEWORK
Sau khi bam OK, hOp thooi New ASP.NET
MVC3 hien re.
ASMEMaiSlaillilinallia
hoject Taupe
Watt doorplate
Va spins
Osaiplia
Addlult/dP.M7 WC 3 ripest.
am& apairiltnisfas
Er Um MIMS walk sash.
El Oen alma praint
Tett project maw
liktklorrw.Tests
Test framework
Votsal Strap Unit Test
- ANOn,l so
1
sao...,..1
Minh 1.7 If(ip thogi New ASP.NET
MVC3
Chting ta co the chon kieu template la Empty, Internet Application ho$c Intranet Application.
Empty se tao ra nhOng thu mut vi t$p tin tei thieu cho an,g dung MVC3.
Internet Application se tao Ira mot img din co 86 tram; m#c dinh, chiing taco the sirs dei
vi airy dung tren n6.
- Tay chon Intranet Application tang tuong to nhu Internet Application, nhung &roc can
filth stn de stl dung trong moi truerng 7LiC than; ngubi clung thong qua ha tang Domain/
Active Directory.
Chung ta tang co the tao project test tong veil Ong dung MVC tir cling mot oh ses.
Ben canh 46, ta tang co the chon chi 40 ViewEngine 11 ASPX ho#c Razor.
(3 day, ta chip Internet Application, Razor ViewEngine vi khong chon unit test project .
Beni OK de hoitn tit.
Trang 21/109
Chuang 1- Tang quan ye ASP.NET MVC FRAMEWORK
Solution Explorer se hien m nhu sau:
g Solution "Avalcreies a moo
• a Isfralowle
D PuMelties
N ltidesenees
r APP-Dete
N CI Content
N
Modds
SaiMs
al Views
•r.' a Globate=
4S Globatesuccs
A Imcka90-031119
D. Web.config
Hinh 1.8 Minh hpa mot Solution Explorer cua ASP.NET
MVC3
Bing duel day me ta nei dung throng dlin mkt met du an MVC Internet Apllication.
Duimg din MO ti
/App Data LAM tit &it lieu dm (mg dung. Chung ta co the them met
CSDL SQL vio thu mkt nly.
/Content Li thu mix throng chits nheng de lieu tlnh, nhu 11 hinh
anh, tep tin css,...
Visual Web Developer al dOng them thu mac Themes vio
trong Content. Thu nut themes 11 thu num chira cac tep
Jquery vi hinh anh. Trong du an nay ta co the iroa thu mac
themes.
Visual Web Developer rang them 1 file CSS chuim vio du
an ten lit Site.css trong thu mix Content. File CSS la file de
mks khi ta muon they doi style caa (mg dung.
/Controllers Chita cac lop controller oft chirc Wing nhijn Sc yeu au caa
ngtrbi ding vi phis holi lai.
MVC yen ciu ten cast tit ca cac file controller ket thee
'Prang 22/109
re.
0111•••11* •
My MVC Application
Welcome to AWAIT MVO
To Wen Men tout 451,,MT MK wok tramaliat
Chuang 1 - Tong quan ye ASP.NET MVC FRAMEWORK
bing the "Controller".
/Models Gam de lop di chin cic mi birth d6i ttryng cia ling dung.
Models quin 1ST vi thao tic voi di lieu.
/Views Litu tit cic file HTML lien quan ten viec hien dil cost ting
dung (giao then Wen ding).
Thu muc Views chits Wang ting mai thu mqc cho n*
controller.
Mic djnh trong thu muc View co 1 thu muc Shared ding
de chia se Si nguyen gitta Sc controller.
/Scripts Thu muc Script hru tilt Sc file Java Script Gila ling ding.
Mic djnh Visual Web Developer se 4t thu mpc niy lem
chuitn de chits cic file Jquery. File "modernize' li file
Javascript de ha trq HTML5 vi CSS3.
filch vd chgy
V6i viec Iva chip Internet Application fru* de, ling dung eta chting ta di c6 dui
chirc Ming ca bin. Ching ta chtty thfr bing cich bim F5 vi kit qui nhtr sau.
Hinh 1.9 Giao diets co ban khi chuy sing dyng ASP.NET
MVC cti san
De dimg chuang trinh, ta t6 hip phim Shift + F5 ho$c chip Debug Menu vi chip Stop
Debugging.
Neu muon thvc hien that' dal trong khi Debug thi ta chin Tools > Option, sau 46:
Trang 23/109
Chuang 1- T6ng quan ye ASP.NET MVC FRAMEWORK
relt and Coto.
6 a bale follt arikaulimen
Iteiarroq epliesThis
ineMe ague Et end Colima
R] invoked by debug coeraniwids
ri Ask find
1•Vam about dale code
EL': Rank code chimes As debugging
Li) Now pecanwiling
r,, Enable Stile debugging an appbcation running wider another user account
rrrol
lust-In-Tone
Dugout ilandow
Symbob
butdiToce
> PIPIVIMPPIO Took
> Outten Took
> NTeen
> frTkl Designer
> Office Teat
> Wags Mains
> SQL Sera Tags
Ten Tomblin.
n
Hink 1.10 Giao diet, thy chink May ddt throng Binh tong khi Debug
1.4.8.3. Kit a 1 cash dfr Wu SQL Server bibsti Entity Framework
ADO.NET Entity Framework II met nen tang dtrqc sar dung de lam viec win CSDL.
Chung ta co the troy win, than tic voi database gain dip thong qua tic dui tircmg trinh.
Chang ta se kit not CSDL bang Entity Framework Database First. No se tin den;
two ra data model va ate lap cho chung ta tit met CSDL co sin. Day la met cich dip can
phO bien vi no thvc hien don giin, nhanh chong.
Trong eaia so Solution click chuOt phii chip Add > New Item...
I-finh 1.11 Katcosh di? ligu bang Entity Framework — hair 1
Tsang 24/109
Genealetlhe modd ban a abbot Qs we paled tea the wadi an the pqect• avid
Ibis wield Slams nay*, delalscoameclisa ad diele aptly Wok Mlle rt.
J Fintsh
Chuang 1- Tong quan ye ASP.NET MVC FRAMEWORK
Tiep theo, chon ADaNET Entity Data Model balm Add
imp lass
• rasa
Code
Gated
we
?hides, Come
WI*
•apetlieg
et NS. LS Tea
sail left
A 1.111. Sra amps
SQL San Caen
nabs
a "Wire
yfraTRs
Wall OP
%SOP
Vaud Cs
%LS Or
rasa
rasa
rasa
Veal Cs
Instaled Tenpldes P
Type vases
• piled NMI is outing. ADOJET
Lily Da DANK
Weight
SQL Saw
%%Mew
Rink1.12 Kilt nth cash die liOu being Entity Framework— by& 2
Criao diein hien ra tint beln du6i, bilm Next
1,/ Dia
.....111.thiCalimb
VIStasold Onsald met
Minh 1.13 left ca sb dit li4u bang Entity Framework— butk 3
Trang 25/109
Chuang 1- T6ng quan ASP.NET MVC FRAMEWORK
ant s6 mai hien ra, to chan New Connection vi thy chqn aic thong so. San d6 htun OK
Enter infommtion to connect to the selected data source or cick "Change to
choose a different data source and/or provider.
...
View
. _
Tra ve mat the hien cis IView ma gqi phuong thirc helper.
Chung to khong can thong tut ve yeu cau o vi du ve phuong thirc helper ben ten, nIumg
chling ta can phiti 'thy dung cac phen tir HTML. Cach de ding that de to HTML trong phuong
thirc helper la sir dung lop TagBuilder. NO la mat phan ciut SystentWeb.WebPage.Mvc nlitmg
sir dung mat tinh ming nhu the la mat phen cua System.Web.Mvc. Chang ta khong can phai
sir dung deu ngoac nhon () doi vet lop TagBuilder. Vi du:
TagBuilder tag = new TagBuilder("u1");
MOt se thanh phin dm lop TagBuilder:
Thanh phan Mo to
InnerHtml Cho phep chimg ta thiet lap nhemg nat dung Gila phin tir nhu mat
chuOi HTML. CtIC gia trj dm thuac tinh khong duqc ma Ma.
SetIrmerText(string) Thiet lap nai dung ciia phan tir HTML. Tham se string dirqc ma
hoa de cho an town khi hien thj.
AddCssClass(string) Them CSS cho HTML
MergeAttribute(string,
string, bool)
Them mat thuac tinh vim HTML. Tham se deu tien la ten dm
thuac tinh, thaw se thu hai la gia trj. Tham so bool de xac dinh
neu eon tai thuac tinh Wing ten thi nen thay the.
Trang SO/109
Chuang 2 - Cac thenh phn chinh cua ASP.NET
MVC
Ket qui tri ve cim phuong thirc HTML la mOt dei tucmg MvcHtmlString. CS vi du ben
tren, ta truyen ket qui cim phtrang thirc TagBuilder.ToString tai met dei tuqng
MycHtmlString mai.
return new MucHtmlString(tag.ToString());
c. Si? dyng HTML Helper có sin.
To cic form
Hai tong s6 nhilng helper duqc sir dung nhieu nhAt lit Html.BeginForm vi Html.EndForm.
Cic helper nay Mo ra cic the form HTML. Vi du clued day cho they vies sir dung nhang helper
nay.
11Html.BeginForm("Index", "Home");}
Html.EndForm();}
Kh8ng cii nio tong sit) nhang helper nay tra ve giit trj ma co the truyen true tiep cho (tau
ra, ta can phii gqi ching ben tong Ichei lenh Razor, lim cho cu phip to nen xau xi. Met
each dip c<in Mt han li sir dung lenh using, nhu sau:
'using (Rtml.BeginForm("Index", "Home"))
{
}
Doan code ten tucmg tu yeti
BeginForm helper do the qui tai, dirge dun ra tong bang clued day:
Qua tai M6 ti
BeginForm0 Tao met form ma gth lai cho action method ma no ce bit
x
nguon.
BeginForm(action,
controller)
Tao met form me giri Mi cho action method vi controller
duqc xic djnh cu the bing chum string
BeginForm(action,
controller,
method)
Gieng phuang thirc qui tai phia tre'n, nhung cho phep chting
ta chi djnh gii ti cho thuOc tinh phuong thirc bang each sir
dung gii tri to System.Web.Mvc.FormMethod
BeginForm(action,
controller,
Giong phuong fink qui tai phia ten, nhtmg cho phep cluing
ta chi djnh nhemg thuOc tinh cho form, mOt 86i Wang cif) cic
Trang 51/109
Chuang 2 - Cac thanh phIn chinh cua ASP.NET
MVC
method, attributes) tinh chat duqc sir dung nhu ten thu6c tinh.
BeginForm(action,
controller,
routeValues, method,
attributes)
_ .. .
Giong phuong thirc qua tai phia'trim, nhung cho phtp thing
to chi djnh cac phan down djnh tuyen trong cau hinh img dung
djnh tuyen, nhtr met d6i tucong c6 cac tinh chat ttrcmg {mg v6i
cac Bien Binh tuyen.
eau:.
@mode“elperMethods.Models.Person
ViewBag.Title • "CreatePerson";
}
CreatePerson
(fusing (Ntnl.BeginForm("CreatePerson", "Home", new { id • "MyIdValue" },
FormMethod.Post, new { class • "personClass", data_formType • "person" }))
{
<div classm adataElem",
PersonId
<div class="dataElem"›
First Name
<div class="dataElem",
clabel>Last Name
}
Vi du tren tut:mg to vOi down code trong HTML nhu sau:
ipos
<form actions"/Nome/CreatePerson/MyIdValue" classm"personClass" data-formType="person"
method="post">
Trang52/109
Chuang 2 - Cac thank phin chfnh caa ASP.NET
MVC
Sir clung Input helper
Bang ducid day the hien nhOng phuong thirc helper co ban c6 sin a tiao ra cat input va
met s6 vi du HTML.
Minh Sian HTML Vi dv
Checkbox Html.CheckBox("myCheckbox", false)
Output:
<input id="myCheckbox" name="myCheckbox" type="checkbox"
value="true" />
Hidden field Html.Hiddenrmyllidden", "van
Output:
<input id="myHidden" name="myHidden" type="hidden"
value="val" />
Radio button Html.RadioButton("myRadiobutton", "val", true)
Output:
<input checked="checked" id="myRadiobutton"
name="myRadiobutton"
type="radio" value="val" />
Password Html.Password("myPassword", "val")
Output:
<input id="myPassword" name="myPassword" type="password"
value="val" />
Text area Html.TextArea("myTextarea", "var, 5, 20, null)
Output:
<textarea cols="20" id="myTextarea" name="myTextarea"
rows="5">
val
Text box Html.TextBox("myTextbox", "val")
Trang 53/109
Chuang 2 - Cac thenh phin chinh cila ASP.NET
MVC
Output:
<input id="myTextbox" name="myTextbox" type="text"
value="val" t>
Si? dyng Strongly Typed Input Helper
Bang chnfri day cho thay nhUng strongly typed input helper co ban va mOt so vi du HTML.
Thanh phan HTML Vi du
Checkbox Html.CheckBoxFor(x => x.IsApproved)
Output:
<input id="IsApproved" name="IsApproved" type="checkbox"
value="true" />
Hidden field Html.HiddenFor(x => x.FirstName)
Output:
<input id="FirstName" name="FirstName" type="hidden" value=""
/>
Radio button Html.RadioButtonFor(x => x.IsApproved, "van
Output:
<input id="IsApproved" name="IsApproved" type="radio"
value="val" />
Password Html.PasswordFor(x => x.Password)
Output:
Text area Html.TextAreaFor(x => x.Bio, 5, 20, new { })
Output:
Bio
value
Text box Html.TextBoxFor(x => x.FirstName)
Output:
Trang 54/109
Chuang 2 - Cac thenh phn chfnh can ASP.NET
MVC
To cac thinh phin Select
Bang dueri day the hi'en namg phucmg thirc helper ma c6 the sir dung de tfio ra cfic
thanh phin Select.
Thanh phan HTML Vi du
Drop-down list Html.DropDownList("myList", new SelectList(new [] {"A", "B")),
"Choose")
Output:
Choose
A
B
Drop-down list Html.DropDownListFor(x => x.Gender, new SelectList(new 0
{"M", "F"}))
Output:
M
F
Multiple-select Html.ListEiox("myList", new MultiSelectList(new [] {"A", "B")))
Output:
A
B
Multiple-select Htnil.ListBoxFor(x => x.Vals, new MultiSelectList(new [] {"A",
ow}))
Trang 55/109
Chuang 2 - Cac thanh phAn chfnh nia ASP.NET
MVC
Output:
A
B
To etc link vi cic URL
Bang duel My the hien nhftng HTML helper cho phop cluing to Ira ve HMTL link vi
URL.
Meta Vi dm
Application-relative
URL
Url.Content("—/my/content.pdf')
Output: /my/contentpdf
Link to named
action/controller
Html.ActionLink("Hi", "About", "Home")
Output: Hi
Link to absolute
URL
Html.ActionLink("Hi", "About", "Home",
"https","www.example.com", "anchor",
new{}, null)
Output: <a
href="https://www.example.com/Home/Aboutlianchor >Hi
Raw URL for
action
Url.Action("About", "Home")
Output: /Home/About
Raw URL for
route data
Url.RouteUrl(new { controller = "c", action = "a" } )
Output: /c/a
Link to arbitrary
route data
Html.RouteLink("Hi", new { controller = "c", action = "a" }, null)
Output: 11i
Link to named
route
Html.RouteLink("Hi", "myNamedRoute", new {})
Output: Hi
u ra cua nhftng helper nay phy thuec vao cau hinh djnh tuyen cua img & sing MVC.
Trang 56/109
Chtrang 2 - Cac thanh phan chfnh cola ASP.NET
?VC
2.2.3.3. SI? dyng Section
Razor Engine hg trq !Mai niem Section, cho phip chung to cung cap cac vimg cua
layout. Razor Section gulp Urn soit tot nhang phan dm View duqc chin vio layout hay
nhUng nth ma chi mg duqc dat. Vi dp sau djnh nglita mot Section:
ViewBag.Title = "Index";
}
This is the view
ieittioe , Hoader {
Iforeach (string str in new [] ("Home", "List", "Edit"})
IHtml.ActionLink(str, str)
}
This is the view between the header and footer sections
@section- Footer {
414>This is the footer
}
ch4>This is the view again
Con day la layout se sir dung section vim mo phia
<link href="prl.Content("»/Content/Site.cse)" rel="stylesheet"
type="text/css" I>
<script src="prl.Content("-/Scripts/jouery-1.4.4.minds")"
type="text/javascript">
penderSection("Header")
This is the layout between the header and the body
Trang 57/109
Chtrcmg 2 - Cac thanh phn chinh nia ASP.NET
MVC
(RenderBody()
This is the layout between the body and the footer
@RenderSection("Footer")
l/body>
Chang ta then nOi dung cita section vao layout bang RenderSection helper. Cac phan con
Gila View khong duqc chira trong Section lit c6 sin thong qua RenderBody helper. N6
then n6i dung tir bit cfr ch8 nio cila View ma !thong chi ra khei @Section.
Day la ket qua 6 layout.
Inds
pox
Mnx/flocaalotianit
Hs= Ltd Eck
This is the layout between the header and the body
This is the view
This is the view between the header and footer sections
This is the view again
This is the layout between the body and the footer
This is the footer
Hinh 2.2 Kit qua sti dung Section
Chit jr: Mtjt View chi co the djnh nghia the section duqc g9i trong layout. MVC
Framework se tang ra ngoai 1g neu chang ta c8 ging dinh nghia nhftng section ma kh8ng
turmg img NAM @RenderSection helper duor ggi trong layout
Kam tra mic Section
Chung ta co the ldem tra xem mOt View co djnh nghia section tir layout hay khong.
Day la cach hieu qua de cung cep nOi dung mac djnh cho section neu nhu View khong cung
cep nai dung xic djnh.
Vi du sau kiem Ira xem section c6 duqc djnh nghia beri View hay khong.
@if (IsSectionDefined("Footer")) {
@RenderSection("Footer")
Trang58/109
Chtrang 2 - Cac thanh phIn chinh atm ASP.NET
MVC
} else {
This is the default footer
}
IsSectionDefined helper nhin ten ciia section muM hem tra va tra ve true neu View
ma chimg ta dang bieu dien dinh nghia section do.
Tra ve the section thy chon
MOt cach khic de kiem tra xem section co duqc dinh nghia hay khong de II Mo ra
section thy chon, nhu sau:
eRenderSection("Header")
This is the layout between the header and the body
penderSection("Body")
This is the layout between the body and the footer
fRenderSection("Footerm, false)
Voi viec truyen tham se false, cluing ta lam cho section thy chip khong bat buOc. Vi
the, layout se tich hop nOi dung neu section &roc dinh nghia, neu !thong no ciing se kh8ng
bet ngoai re.
2.2.3.4. Stir dim Partial View
Partial View la nhung doan nOi dung co the tai sir dung va cac doan code co the nhimg
vao nhang View khic, glop nang cao kha thing sir dung cua trang web, giam cac ma" rang
Tao mOt Partial View
Chang ta Mo Partial View bing cach click chuOt phai vio thu muc View, chon Add >
View va sau do chon vac) muc Create as a partial view.
Trang 59/109
Chuang 2 - Cic thanh phan chinh cue ASP.NET
MVC
View name
MyPotial-
Vas engine
Et Crate a strongly-Wed view
Medd do a
1 L
Suitakl Wapiti
[Empy__
____ _ _ _ i, [0 Reference script Manes
sa Cade n • putiohne•
I] Use a layout or master page
•
1 r
(Lon empty' k is pet in • RaOLtherilallift
CententPleceHolder ID:
IiiinCentee. I
WI.
Hinh 2.3 Tao nkit Partial View
Tri ve Partial View
Cac phucrng thirc Partial, RenderPartial, RenderAction helper duct sir dung de tra ve
Partial View tong Razor.
ptml.Partial("_Conments")
pOtml.RenderPartiagm_Coments");}
Diem lchic biet chinh gifra 2 phucmg that phia ten la phucmg thirc Partial helper tra
lei partial view thinh mOt chugi. Trong khi phucmg that RenderPartial viet Ulm tiep vio
luting phan img thay vi frit ve mOt chugi.
WIttml.RenderAction("_Category","Home");}
Chu 9:
Phuong thirc Partial, RenderPartial duqc sir dung khi model dA duct diet day du
thong tin. Vi du, trong mgt blog hien thj birth luon Cila bai viet, chimg to sir dung Partial hoc
RenderPartial tir mOt thong tin c6 sin la bai viet 6 model.
Trong 60/109
Cturcmg 2 - Cac thanh phan chinh cua ASP.NET
MVC
Phuong thirc RenderAction duqc sir dtmg khi thiing tin can duqc hien thj ar nhieu
trang. Do do, partial view can co model rieng. Vi du, dash sach the loci cita bai viet o tat ca
cac trang, chinag ta nen sir dung phtrcmg thirc RenderAction.
223.5. St? dyng Child Action
Child Action la cac action method duqc gui tir ben trong View. Dieu nay ghip cluing
ta tranh viec trang lap controller logic ma sir dung 6 mot se not trong img dung.
Tgo m(it Child Action
Bit kjf action nio cling c6 the sir dung nhu la child action. Vi du dueri day bieu dies
Mc diem Gila child action.
[ChildActionOnly]
public ActionResult Time()
return PartialView(DateTime.Now);
}
ThUOC tinh ChildActionOnly dam bio ring mot action method chi co the duct g9i
nhu la child action ben trong View. Mot action method khong an thuijc tinh nay de trer thinh
child action, nhung cluing ta co xu huting sir dung no a ngen chin cac action method duqc
den ra nhu ket qua tir you eau ctia ngubi dung. Sau khi djnh nghia action method, cluing ta
ph& xac dinh nhang gi duqc trit ve !chi action duqc dln ra. Child action thtdmg duct kit hop
yeti partial view, mac clit lchong bat buOc. Vi du ve Partial View de sir dung child action nhu
sau:
model DateTime
The time is: .Model.ToShortTimeString(Wp>
Trii vi mt)t child action
Chung ta c6 the den ra child action sir dung Html.Action helper. Vei helper nay,
action method &Km thtrc hi 'en, ActionResult duqc xir ly, va dau ra duqc truyen cho phia
client.
11{
Vieweag.Title = "Child Action Demonstration";
}
TYang61/109
Chuang 2 - Cic thenh pit chiral caa ASP.NET
MVC
Th1s is the method rendered by the action method
OHtml.Action("Time")
This is the method rendered by the action method againqp>
Ket qua la
Chid Action Dernonststation
ir 0 X
This is the method rendered by the action method
The time is: 19:47
This is the method rendered by the action method again
Hinh 1.4 Minh hpa kit qua Ira 4 mot Child Action.
Vi du tree, ta sir dung mOt them so de cung cap ten coa action method se den ra.
Ngoai ra, chimg ta ding co the gi action method tit controller lchac bing viec cung cap them
controller name, nhu sau:
@Html.Action("Time", "MyController").
2.3. Model
Trong ASP.NET
MVC Framework, model chju trach nhiem cho phan lOi coa ring
dung va cac tic vu lien quan den du lieu. Cu the la ket not den ca so der lieu va thirc hien
cac thao tic tren dii lieu do. Model con co vai fro mo ta cac thuc the trong dui lieu, ket hqp
nhieu loci dU lieu yeti nhau theo yeu cau ciaa View. Ngoai ra con c6 the gin mgt model
vao view de hien thj va xac Whin der lieu.
Trang 62/109
Chuang 2 - Cac thenh phAn chinh cua ASP.NET
MVC
2.3.1. Model Binding
Model binding la qua trinh khoi tao dei Wong .NET sir dung der lieu duqc girl bang
trinh duyet (browser) trong met you cau HTTP. MVC Framework phai atilt xa den chi tiet
ciia yeti eau then each ma no co the truyen du lieu thich hop ho4c tham se dei Wong toi
action method, sir dung model binder Sc djnh (DefaultModelBinder).
2.3.1.1. 71m hien y? Model Binding
Gia sir rang chting to dA djnh nghia met action method trong controller nhu sou:
using System;
using System.Web.Mvc;
using MvcApp.Models;
namespace MvcApp.Controllers
{
public class HomeController : Controller
{
public ViewResult Person(int id)
{
// get a person record from the repository
Person myPerson = null; //...retrieval logic goes here...
return View(myPerson);
}
}
}
}Chi nhen met you cau cho URL nhtr Home/Person/23, MVC Framework phai anh xa
chi tiet elm you cau bang each truyen met gia tri thich ho4c (lei Wong thich hop (tham se) tan
action method.
Action invoker - thinh phen ma dam ra action method, chill teach nhiem co duqc gia tri
cha the tham se trthic khi no dam ra action method. Action invoker Sc djnh la
ControllerActionInvoker, duo vat) cac model binder — duqc djnh nghia bei giao diem
ImodelBinder.
namespace System.Web.Mvc
{
public interface IModelBinder
Trang63/109
Chuang 2 - Cac thenh phin chinh cua ASP.NETC
{
object BindModel(ContllerContext controllerContext,
ModelBindingContext bindingContext);
}
}
C6 the c6 nhieu model binder trong met (mg dung MVC, mai binder co teach nhiem
rang buk met hoc nhieu loai model. 'Chi action invoker can gqi met action method, no nhin
vao tham se ma duqc method dinh nghia va tim model binder c6 nth nhiem cho mai loci
tham se. (3 truang hqp vi du ban ditu, action invoker se tim action method ma c6 met tham se
int, vay ne se xk djnh frith nhiem binder di rang buec gia trj int va gqi phuong thirc
BindModels cita no. Neu khong c6 binder nito hog deng bin gia tri int, model binder n*
dinh se duce sir dung.
Met model binder co trach nhiem tao ra sac gia tri tham s6 action method phi' hqp.
Dieu nay thuemg c6 nghia la chuyen dai thanh phin cua de lieu you cau (Sur la gia tri form
hoac chugi query), nhung MVC Framework khong dot gi6i han ve du lieu thu duqc.
2.3.1.2. Sir dyng model binder m4c dinh
Mac dumet ung dung co the c6 nhieu binder, nhung phan 16n deu clua tren lap
binder c6 s'an la DefaultModelBinder.
Default model binder se tim kiem theo bang duOi day.
Source M8 tit
Request.Form Chita dff lieu form HTML.
RouteData.Values Gia tri djnh tuyen cac ling dung.
Request.QueryString De lieu tong chuai query cita URL.
Request.Files Nhang tep tin duce upload nhu met phin dm yeu au.
. De tun mqt thaw so id":
• Request.Formridi
• RouteData.Valuesridi
• Request.QueryStringridi
• Request.Files["id"]
Trang 64/109
Chuang 2 - Cac thanh phAn chfnh caa ASP.NET
MVC
Ring buec nhttng hien tham s6 don Win
!Chi lim vier vbi nheng ldeu tham so don giin, DefaultModelBinder co ging chuyen
doi gii tri churn mi dtrqc thu thip tir de lieu y8u eau sang kieu cita tham se, sir dung lop
System.ComponentModel.TypeDescriptor.
Neu gib tri khong the chuyen d61, vi du nhu chung ta cung cap gii trj kieu "Qui tio"
cho tham s6 mi you ciu co gii tri kieu int, DefaultModelBinder khong the rang buec tai
model.
Chung ta co the sirs di tham s6 de trash SeP phiti yin de niy. Chting ta sir dung kieu
nullable, nhu sau:
public ViewResult RegisterPerson(int? id) {
Neu citing ta sir dung cacti flip thy, tham se id se null neu lcheng khdp yeti dtt
lieu chuyen di di they trong you ciu. Ngoli ra, cluing ta ding co the lim cho tham so fret
thinh thy chqn bang each cung cap gii tri
djnh neu nhu khong co de lieu sin co, nhu
sau:
public ViewResult RegisterPerson(int id = 23) {
Ring bu4c nheng Wen tham s6 phirc ttip
Khi tham se ciut action method có kieu phirc tap (nheng kieu de lieu mi kh8ng the
chuyen doi bing each sir dung lop TypeConverter), lop DefaultModelBinder sir dung the
phAn chieu de thu duqc tap hqp the thuec drill public vi ring buec lin lugt trong se do.
Vi du:
public class Person
{
[HiddenInput(DisplayValue = false)]
public int Personld { get; set;
public string Firsthame { get; set; }
public string LastName { get; set;
[DataType(DataType.Date)]
public DateTime BirthDate { get; set;
public Address HomeAddress ( get; set; }
public bool IsApproved { get; set; }
public Role Role { get; set; }}
Trang 65/109
Chuang 2 - Cac thAnh phIn chfnh tha ASP.NET MVC
Default model binder se kiem tra cic thuOc tinh xem lieu chi mg ce, la kieu de lieu
don gian. Neu dung, binder se tim muc de lieu (data item) trong you cau c6 cling ten Arai
thuOc tinh.
Neu ck thuec tinh la kieu phfrc tap khic, thi qui Binh nay duqc lap Mi cho kieu mai,
met bt) cic thuec tinh public Quay thu thop, ya binder lei co gang tim gii trj cum tit ca. Str
khac biet la ten thuOc tinh duqc icing nhau. Vi dp, thuOc tinh HomeAddress dm hip Person la
thuOc kieu Address du& day.
public class Address
{
public string Linel { get; set;
public string Line2 { get; set; }
public string City { get; set;
public string PostalCode { get; set;
public string Country { get; set; }
}
2.3.1.3. Got Model Binding theo cach thd cling
Qua With model binding duqc thuc hien ty dOng khi action method xic dinh &Arc dm
thaw sit), nhtmg chting ta cling c6 the lciem soit tn,rc tiep qui trinh neu muen. Dieu nay glop
cho chfmg ta lciem soar re rang hen ye each kheri to don man model, nut me de lieu duqc
thu Mk), ya lam the nao cic lei duqc xi: ly. Vi dp duai day cho they ckh cluing ta thay din
action method Address a HomeController a gqi qui tinh model binding theo cich thu °kg.
(ittpPost)
public ActionResult RegisterMember()
{
Person myPerson new Person();
UpdateModel(myPerson);
return View(myPerson);
}
Phucmg thqc UpdateModel nh(lit them se la del arcing ma chimg ta cTh tao pink de ya
co gang thu thap gii trj cho thuOc tinh public dm n6, se dung qui frith rang buOc
Trang 66/109
Chutmg 2 - Cac thanh phAn chfnh ctia ASP.NET
MVC
IChi chUng to to gai qua trinh ring budc, cluing to co the giai hon. qua irinh ring budc
tai mOt nguiin du lieu don. Mac djnh, binder tim kiem trong bOn nai: dit lieu form, dir" lieu
djnh tuyen, chai query, va nheng tep tin duqc upload.
Vi du duel day cho they each giai hp de binder tim kiem trong mdt tri (Inning
hop nay la de lieu form):
[HttpFost]
public ActionResult RegisterMember()
{
Person myPerson =
(Person)DependencyResolver.Current.GetService(typeof(Person));
UpdateModel(myPerson, new FormValueProvider(ControllerContext));
return View(myPerson);
}
Phuang that UpdateModel Ian nay nhan mOt su dive hien caa giao din IValueProvider,
mi se tits thinh ngu6n duy nhat coo gia da cho qua trinh rang budc. Mtn hang 4 vi
tri dit lieu duac doi dies bdi mdt su thut hien IvalueProvider, nhu sou:
Nguilin Su that thi IValueProvider
Request.Form FormValueProvider
RouteData.Values RouteDataValueProvider
Request.QueryString QueryStringValueProvider
Request.Files
.
HttpFileCollectionValueProvider
_ Moi . .. lap uqc . e phia tren nhan mi.,* them so xay dung ControllerContext, ma
chting to có the thu duqc tir thudc tinh Controller có ding ten, nhu duqc the hien trong
danh sich.
Cach phi) bien What cue viec giai hart nguOn de lieu la chi xem xet duy nh,4t cac gil tri
form. Co mdt thu that rang budc gin ging co the se dung ma Idging can too the hien cue
FormValueProvider, nhu sau:
[HttpPost]
public ActionResult RegisterMember(FormCollection formData)
{
Trang 67/109
Chuang 2 - Cac thanh phIn chfnh c0a ASP.NET
MVC
Person myPerson
(Person )DependencyResolver . Current .GetService(typeof (Person) );
UpdateModel(myPerson, formData);
return View(myPerson);
Lap FormCollection thuc hien giao dien IValueProvider, vd neu chting to djnh tic
action method de nhan tham s6 cua ldeu nay, model binder se cung cap cho chi mg ta met CI&
tutmg c6 the truyen Ulm tiep tOi phuang thirc UpdateModel.
XIII, v61181 ring buOc (binding)
Nguai sic dung se khifing tranh !dick vier tong alp gia 14 ma kh8ng the dugs ring
buec vOi thuOc tinh model twang ung, nhu Ia ngay khong hop le, kieu text cho gia trj se. Kfu
thing ta gqi model binding met each re ring, chitin ta phii co tech nhiem xti 19 yeti bat kk
181 mkt d6. Model binder the hien cac 16i ring buOc bing each nem ra met ngoM le
InvalidOperationException. Chi tiet ve cac 16i c6 the tim ra thong qua tinh rang ModelState.
'Chi sir dung phuang thirc UpdateModel, chting ta phiti chuitn bj de bat ngoai le .vi sir dung
ModelState de the hien met thong &to 16i cho ngtrai sir dung. Nhu vi du sau:
(HttpPost]
public ActionResult RegisterMember(FormCollection formData)
{
Person myPerson =
(Person)DependencyResolver.Current.GetService(typeof(Person));
try
( UpdateModel(myPerson, formData); )
catch (InvalidOperationException ex)
{
//...provide UI feedback based on ModelState
}
return View(myPerson);
}
Met phuimg phip kink II sir dung phuang thirc TryUpdateModel. Hwang thirc nay
tra ve gia trj true neu qua trinh model binding thinh tong vi tra ve gia trj false neu c6 16i.
Nhu vi du sau:
(HttpPost]
Trang 68/109
Chtrcmg 2 - Cac thanh phin chfnh cAa ASP.NET
MVC
public ActionResult RegisterMember(FormCollection formData)
{
Person myPerson =
(Person)DependencyResolver.Current.GetService(typeof(Person));
if (TryUpdateModel(myPerson, formData))
{
//...proceed as normal
}
else
{
//...provide UI feedback based on ModelState
}
}
Ljt do duy nhat de uu tien TryUpdateModel han UpdateModel 11 khi chang ta khong
thich bit va xir ly veri ngoai 10, ngoai ra kh8ng c6 su khic biet tong qua trinh model binding.
Chit Y: !Chi model binding Quqc 8qi to deng, cac 161 rang butjc se khong ra hieu v6i
ngoai thay vao Q6 chang ta phai kiem tm ket qua thong qua thu6c tinh ModelStatelsValid
2.3.2. Model Validation
Model Validation 11 qua drib dam bio dO lieu m1 chimg ta nkin duqc phi hap di
ring buoc vei model caa thing ta. N6 con cung rip nhOng thong tin hUu ich Qe giap ngubi
sir dung khic phuc dirge van di.
PhAn dAu caa qua trinh la Iciern tra chi lieu nhan duqc. Day la met trong nhOng cash
Chung ta bao ve tinh man vgn rim domain model. Bing cash loM be) nhOng adieu kh8ng c6
nghia trong domain model, thing ta nein chin doge nheng tang thai ky lu vi khong mong
mutin phat sinh trong ling dung. Plain thir hai quan tang khOng kern, d6 11 giap de( ngubi sir
dung khic phuc 18i. Neu chang ta khong cung cap cho ngubi sir dung thong tin vi tong cu di
ha twang the yeti ling dung theo each ma chimg ta an ha lam, ha se fret nen bac bei va nham
an. Din din ket qua khong mong main.
May min la MVC Framework cung cap than 116 trq rang rai cho model validation.
Trang 69/109
Chtrang 2 - Cac thanh phAn chinh cua ASP.NET
MVC
2.3.2.1. Mc nhdn (Validate) trong action method
Trong action method, thing to co the xac nh(bi model. Ta c6 the hem tra giri tri thuOc
tinh cua din tuqng model va ding 14, bit kt 16i flan tim thiy thong qua thuoc tinh
ModelState.
Vi du:
if (string.IsNullOrEmpty(mode1.0serName))
{
ModelState.AddModelError("UserName", "Please enter your user name");
if (Imodel.TermsAccepted)
{
ModelState.AddModelError("TermsAccepted", "You must accept the terms");
}
if (MOdelState.IsValid)
{
//send model for further processing
}
Ngoid ra, to co the Iciem tra xem lieu model binder co the grin met gia tri cho thuOc tinh
bang phtrung thirc ModelStatelsVaildField. Lam 4y de darn brio ring model binder co kha
!ling phaln tick di) lieu ma ngubi clung di girl. Vey nen c6 the thus hien de Idem tra bi sung.
2.3.2.2. Sir dyng ValidadonSummary
ValidationSummary hien thi thong brio
ma chung to di ding IcS, voi ModelState
trong action method. C6 met so qua tai cim phuong that ValidationSummary, nhu sau:
Phweng thirc Mo ni
Html.ValidationSununary0 To ra met tom tit chung cho tit ca cac 16i x.ac nhen.
Html.ValidationSummary(bool) Neu tham s6 bool tra ve true, chi duy nhit 18i model-
level duqc hien thi. Ngtrqc lai, tat cit cac ltii deu
dupt hien thi.
Html.ValidationSummary(string) Hien thi thing brio (chin trong tham so string) Inset
met ban tom tit tit ca cac lii.
Trang70/109
Chtrang 2 - Clc thanh phIn chtnh ctia ASP.NET
MVC
Html.ValidationSurtunary(bool,string)
Hien thj thong bao truerc khi c6 loi 'Lac thin.' Neu
tham s6 bool tra ve true, chi duy nhit 1& model-level
duct hien thj.
2.3.2.3. Sir dyng ValidationMessage
Helper Html.ValidtaionMessageFor hien thi cac 16i xac thin cho mai thuac tinh
model don. Vi
@Ntml.ValidationMessageFor(m .> m.UserName)
2.3.2.4. X6c nhOn trong Model Binder
Default model binder thuc hien xac nhen nhu mat qua trinh binding.Model binder thqc
hien mat so xac Whin ca ban cho mai thuac tinh trong dei tuqng model. Neu gia tri khang dugs
cung cap hoic lchang duqc phin tich tea ldeu thuac tinh model, thi DefaultModelBinder
cung cap cho cluing ta mat se phutmg thin hem ich ma c6 the chide a them xac Whin cho mat
binder. Chi tiet 6 tong bang dual dity.
Ten phuong thfrc Ma tit
Stir thuc thi mic dinh
OnModelUpdated DlIQ'C gqi khi binder co ging
On gia tri cho tat ca thuac
tinh trong doi luting model.
(Model-level Validation)
Ap dung quy tic xac nhan
duqc Binh nghitt bad model
metadata va ding ky bit ky
lai nio v6i ModelState.
SetProperty Duqc gqi khi binder muen
ap dung mat gia tri tad thuac
tinh cu the. (Property
Validation)
Thong bao duct you au
hoic khan ghtm le se duce
hien thi.
2.3.2.5. C6c ch6 Mich t ►? lieu (Data Annotations)
MVC Framework ha trq vies sir dung metadata de the hien cac quy tic xac nhin
model. Chang ta co the de ding them xac nhin' cho (mg dung dm minh bing cach them
khang gian ten Data Annotations vi sir dung cac ac tinh cua no cho cac 16p model. Data
Annotations cho phdp chting ta ma ta cac quy tic duqc ap dung cho cac thuac tinh model.
Trang 71/109
Chuang 2 - Cac thanh phIn chfnh min ASP.NET
MVC
ASP.NET
MVC se lo viec thi hanh chimg va hien dti nhOng thong bao thich hop cho ngutti
sir dung. C6 mOt so lugng cat 16p die tinh ke thin lop dac tinh xac than, sir dung de quart ly
cac loci xac nhan khac nhau.
MOt so diem quan tong trong cac die tinh data annotation la:
• Required: chi ra ring thuOc tinh la throng can thiet.
• DisplayName: Dinh nghia doan van ban ma chimg ta muon sir dung trong cac form va
thong bao xac nhan.
• StringLength: Quy djnh de dai tin da cho met throng lcieu chutd (string).
• Range: Cling cep gia tri tOi da va ten thieu cho mOt se tzuong.
• RegularExpression: Xac nhan bieu thilt du vio nhu mOt bieu thirc chinh quy.
• Compare: 2 thuOc tinh phai c6 cimg gia tri.
2.3.2.6. Thy chlnh l6p Validation
Chung ta co the too ra cac ditc tinh xac nhim tay chinh bing each din )(nit fir 16p
ValidationAttribute va thuc hien xac nhan logic tha rieng minh. Ta cling co the ghi de len
phuang thirc IsValid cu...a nhOng trang web twang tat cue tot va nhitu fink thing. Thu vien jQuery
Trang 79/109
Chtrcmg 2 - Cic thinh phan chinh ctia ASP.NET
MVC
loci be stir phirc tap cita hinh duyet trong qui trinh nay va cho phep nguai phut trien web
c6 the tip hung vao cic tinh Wing dau cued.
2.5.2. Co bin ye jQuery
Ham quan tong cita jquery la Jquery0. Chung ta c6 the sir dung no de troy van tit ca cac
thanh phan eau tnic cay (DOM = Document Object Model) thong trang HTML lien kit voi
yeti css.
Vi du:
jQuery("DIV.MyClass") no se tim tit ca the div trong trang c6 sir dung class css la
MyClass. jQuery("DIV .MyClass").hide0 lam cho tit ca cac the div kit hop YIN class
MyClass trong css bien mat.
BE tin lgi ham, jQuery cung cep mOt ctm trtc ngin gon cua jQuery0 de lit $0.
MOt so vi
$("P SPAN'D.addClass("SuperBig") cQng class css SuperBig vao tat ca tat ca cac the
dirge chim tong the .
$(".SuperBig").removeClass("SuperBig") removes class css c6 ten la SuperBig tir cac
tag c6 class SuperBig trong file css.
- S(#options").toggle(). Bit tit hien thi phan tir vii id cha tag la option. Neu tag clang
hien thi thi n6 se in tag di va ngtroc 1 i n6 se hie'n tag len.
$("DIV:has(INPUT[type—'checkboxl:clisabled)").prepend("Hey!") chin tag
Hey! vao diu cita tit ca cac the div chile checkbox duqc disable.
- $("#options A").css("color","red").fadeOuto tun tat ca cac the lien kit co id la
option, they the color cia text thanh mitu do vit thay doi opactiy ve 0.
2.5.3. jQuery tich hyp trong ASP.NET
MVC
Mei project ASP.NET
MVC dieu duqc tich hop thu vien jquery trong folder /Script.
Citing nhu nhieu thu vin javascript khac, no nhu la mfit file .js. DE sir dung chting ta can
tham chit% den n6. VI du, trong file layoutcshtml ta chin down code sau.
<script srcelprl.Contentr-/Scripts/jqueryl.5.1.min.jertype="text/javascript"›
Trang 80/109
Chuang 2 - Cac thenh phIn chfnh via ASP.NET
MVC
2.6. Rao mOt trong ASP.NET
MVC
De phut hien met Ong dung web, chting ta can phiti co nhan thin can this& ve van de bito
mat web a cap do you cau va plan hal HTTP. C6 nhiau cach khic nhau co the Can &mg vio
met Ong dung web, nhu la cross-site scripting (XSS), gia mao yeti cau (CSRF), hay SQL
injection. Tuy nhien, neu hieu hitt kk va nhang mei de km nay, chting ta hoin toin c6 the
giam thieu nguy cc bi tan Gong. Sau day la met se phuang thin tan cong pha bien.
2.6.1. Cross-Site Scripting
a. Khali niem
Cross-Site Scripting (XSS) la met trong nhOng Id thuat tan ding Icha phi) hien,
clang thei n6 cling la met trong nhang yen de bao mat quan tong dei vol cac nhi ph& tilt
web va ca nhOng ngtrai sir dung web. Bit ki mat website nao cho phep nguari sir dung
(Ling thong tin ma khong c6 sir kiem tra chat the cac doan ma nguy hiem thi dau c6 the
tiam An cac loi XSS.
Cross-Site Scripting hay can duqc gqi tit la XSS (thay vi gqi tit la CSS giap Minh
nham lan voi CSS-Cascading Style Sheet dm HTML) la mot Id thuat tint cuing bang citch
chen vat) cac website clang nheng the HTML hay nhOng doan ma script nguy hiem co the
gay nguy hal cho nhOng ngtrbi sir dung khic. Trong dd, nhOmg clop ma nguy hiem ducrc
chen vim him hit duqc viet bang cac Client-Site Script nhu JavaScript, JScript, DHTML vi
Ong co tilt la ca cac the HTML.
b. Hotit dOng mia XSS
Va ca ban XSS la cac yeu au request dugs girl tir cac may client ten server nham chen
vio de cac thong tin vugt qui tam kiem sok dm server. No co the la met request &cc girl tir
cac form do lieu hoac ming c6 the chi la cac URL nhu 11:
-alert ('XSS was found r)
va rat c6 the trinh duyet cim chting ta se hien len mat then halo "XS'S was found! ". CfiC doan
ma tong the script khong he bi glen han bed cluing hoin toan c6 the thay the lag met file
ngulin ten met server khic thong qua thuOc tinh src cfla the script. CiuThg chfnh vi le do ma
cluing ta chua the lubng het duqc dt) nguy hiem ciza cac 18i XSS.
Trang 81/109
Chuang 2 - Cac thanh phAn chinh clia ASP.NET MVC
Tuy nhien, neu nhu cac ky thuat tin pang khic co the lam thay doi duqc de lieu
nguOn ciut web server (ma nguen, CSDL) thi XSS chi gay Mn hai vesi website 6 phis
client ma nan nhan true tiep la nheng khich duyet website de. XSS la nhtmg script phia
client, nheng down ma nay se chi chay bbl trinh duyet phis client do do XSS !thong lam anh
hueng den he thong website nim tree server. Muc lieu tin cong cita XSS khong ai khic
chinh la Stung khich hang cita website, khi hq v8 tinh vio cac trang c6 chira cac doom me
nguy hiem do cac hacker de lii. Ho co the bj chuyen tbi the website !chic, dat lei homepage,
hay nay% ham la mit mat khiu, mat cookies then chi may tinh ctia hq di the se bj cai the 1*
virus pha boat.
c. Giii phip chong lgi XSS
Nhu di de cap 6 tree, XSS chi c6 the tin cong khi girl tea trinh duyet web cita nan When
met lien ket c6 chiza (Than ma script a hal. Chung to c6 the bito ve khach hang sir dung
website bang cach lqc , xic nhan hqp 19 de lieu vio tir phia khach hang va lctc gia tri tra lei
Vei ASP.NET
MVC3, ching to co the tan dung co phip tir deng ma has @ cia Razor View
de giam thieu ding k'e nguy ca tin cong tir XSS.
2.6.2. SQL Injection
a Kali niem
SQL Injection la met trong nheng kieu hack web di; tr6 nen phi) bien hien nay. Bing each
inject cac ma SQL query/command vio input fru& khi chuyen cho ung dung web 7th li,
hacker c6 the login ma kitting can usemame va password, remote execution, dump data vi
lay root cita SQL server. C8ng cu dung de tin acing 11 mot trinh duvet web bit ki, chang hen
nhu Internet Explorer, FireFox,Chrome...
b. Meng nai thwoug bj tin cong.
Nhang nai thutong bj tin ding de la cac form dAng nhAp, tim kiem, lien
Nhiing form
nay la nai tiep Lilian de lieu va se duqc dim vio trong de xir 19.
Thu submit cac field usemame, password hoc field id, .. bang hi' or 1=1-
- Login: hi' or 1=1-
- Password: hi' or 1=1--
rrang 82/109
Chtrcmg 2 - Cac thenh ph3n chinh &la ASP.NET MVC
-
or 1=1—
Neu site chuyen tham s6 qua field an, hay download source HTML, km ten iffa cimg vi
thay dei lai URL cho phe hep. Vi du:
<FORM action=
method=post>
Neu thanh cong , thi c6 the login vio ma khong can pith biet usemame va password.
c. Lf do' or 1=1— to the vuvt qua phln idim tra ding nb$p
GUI sir nhu co met tang ASP lien ket den met ASP tang kbac yeti URL nhu sau:
- Trong URL tren, bien 'category' duqc gin gia tri la 'food'. MA ASP du trang nay cog
the nhu sau:
v_cat = request("category")
sqlstr="SELECT • FROM product WHERE PCategory='" & v_cat & "'"
set rs=conn.execute(sqlstr)
- v_cat se chua gia tri ctiia bien request("category") la 'food' vi cau l'enh SQL tiep theo
se la:
SELECT * FROM product WHERE PCategory='food'
- Deng query ten se tra ve met tgp resultset chira met hoc nhieu dung phu hep voi
dieu kien WHERE PCategory='food'
- Neu thay del URL tren thinh
or 1=1-
- Bien v_cat se chira gia trj "food' or 1=1— " vi clang ler& SQL query se la:
SELECT * FROM product WHERE PCategory=sfood' or 1=1--'
Deng query tren se select mei thu tong bang product bit chap gig tri cila twang
PCategory c6 bang 'food' hay kheng. Hai diu gash ngang (—) thong bac) MS SQL
server biet mei thir con lai sau "—" se bj be qua (coi nhu la phan chti thich). Doi veil
MySQL, thay "—" thinh "#"
Trang83/109
Onrcmg 2 - Cac thenh phIn chinh ctia ASP.NET
MVC
Ngoid ra, cling có the thin each khic bang submit' or sal= 1a. Deng SQL query bay gib
se la:
SELECT * FROM product WHERE PCategory='food' or 'a'='a'
- Med so loai dft ligu Ichic ma cling co the thin submit de biet xem trang web co gap 18i
hay khong:
• or 1=1—
• or 1=1--
• or 1=1--
• or 'a'='a
• - or
• ') or ('a'-'a
d. Giai phip phang chong SQL Injection
Loai be Cie ki to meta nhir '"A; va cac ki tir extend nhu NULL, CR, LF, trong the string
nhan dirge tic
- Input do ngued dung nhap vac,
- Clic tham so tir URL
- Cac gia tq tir cookie
Viec sir dkmg LINQ trong ASP.NET
MVC s8 ming cao viec phi:mg chOng SQL Injection
beri LINQ truyen thong tin ton CSDL thong qua cac thaw so SQL.
Trang 84/109
Chuang 3 - Chuang trInh frng dung
CHM:MG 3 - CHU'O'NG TRINH DING DUNG
3.1. M6 ti bit Min
ling dung li met website nhim dip irng nhu cau gieri thi'eu san pham va (lag hang truc
tuyen cua hieu sach Cat Tubng. iYng dung can duqc to chin tren ca sec de lieu hqp 19.
Ngoii ra, no phil cung cap nhOng chirc ding can thiet cho viec quin lY san pham trong
hieu sach ding nhu giao dich veil khach hang.
3.1.1. CAc 461ttryng tham gla van ht thong
Co 3 loci dei tuqng sir dung chinh:
- Khach Wing lai (guest): Li nhemg nguiri chug clang ky tai khoan. HQ co the xem thong
tin tren tang web. Tuy nhien nhftng doi tucmg nay khong the dux hitt dit hang vi
met so chic ning khac;
Minh vier (member): Li nhftng ngutti di ding kY Lai khoan. H9 co the dit mua cac
san pham truc tuyen vi thuc hien met so cac chic nAng khac;
Quail tri thong (admin): Li ngtrei co toan quyen quin 1Y, xir 15, cac hog den dui
he thong.
3.1.2. Ck chat flung chinh ala frng Ong
- Quin 1St ngubi dung
- Quail ly nha xuat bin
- Quail ly loai sach
- Quin ly sach
- Quan 15, boa don
- Dit hing Mix tuyen
Trang 85/109
Clurcmg 3 - Chtrang trInh frng dung
3.1.3. Str dB use-case chirc nang ctia ht thong
3.1.3.1. Chat nand cue adman
Hinh 3.1 Sa do use-case el:tic ndng cue admin
a. Ding nh(ip
Tnrdrc tien, admin pith thuc hien ding nhOp de quan 1y he thong.
b. Quin If nguiri ding
OS ring nay cho phip admin them, sfra, x6a, phan quyen mot tai khoin tren he thong.
c. Quin If nhi nit bin
Chirc nAng nay cho phep admin theo deg dash muc cite nhi xuat ban c6 trong he thting.
Ben cash do, admin co the them, sfra, x6a thong tin yea nha xuat ban.
d. Quin If thi
Chic nAng nay cho phdp admin them, stia, x6a, theo dbi cite loci sach.
e. Quin If sich
Chirc ring nay cho phip admin them, situ, x6a, theo del cite son pheit n sada.
f. Quin h6a dun
Chic nAng nay cho phdp admin theo dal nhOng don dot hang clia khach, cop nhOt the
tang thai dia hem don. Vi
nhon la hes don duqc at, a thanh total, da giao hang
Trang 86/109
3Com don hang
Chiang 3 - Chuang trinh Cmg dung
3.1.3.2. Chat ming dm khach hang
7c at hang
Math hang
Hinh 3.2 So. cid use-case char nang caa ripe& dung
a. Ding Ict di khan
Khich hang co the dAng ItY mOt di khan de trey thanh thank vier cua he thOng.
b. Ding uhep
Sau khi co dugc mOt tai khan, khich hang duty hien viec dAng nh#p de co the d#t hang
tren he thOng. Ngoii ra, khich hang co the thay doi mkt khAu truy c#p caa minh.
c. Dkit Mug
Cho phep Ichich hang d#t mua mOt hoc nhieu san phArn ca he tong. Chi ap dung khi
khach hang dA ding nh3p.
d. Xem dun hang
Sau khi ding nh#p, chirc nAng nay cho phop khach himg Idem tra 1M don hang ma minh di
d#t v4i nhiing thong tin cu the. Khach hang co the hay don' hang chua giao djch neu mute.
Trang 87/109
lath
math
molosi
math
bleach
namontben
Shinn.)
mats
Noah
Int
mot03
ten*
thong*,
I rnalo ----
Unica
mete
D
tan&
meths
mensinh
giainh
*ma
ass
trangthai
nth
headonct
maw
9 mad
Won.
Kiss
hoodoo
Chuang 3 - Chuang ttinh Caw clung
3.2. T8 chin co s& dfr litu
3.2.1. M8 hinh ca s& de Btu
Hinh 3.3 MB hinh co. so. del lieu cho ung dung
'Prang 88/109
Chtrang 3 - Chtrang trInh frng dung
3.2.2. Chi tiEt cac bang trong ca s& WI lltu
ID INT MA lchitch hang (kb:a chinh)
tendn NVARCHAR 30 Ten clang tihk•
matkhau NVARCHAR 100 Mat u truy cap
namsinh INT Nam sinh
gioitinh TINYINT Gieri tinh •
email NVARCHAR 500 Email cua khArh hang
diachi NVARCHAR 500 Dja chi ala khitch hang
trangthai TINYINT Trang thai cita tai khofin. 1 la binh
thuong, -1 II bj khea
role TINYINT Quyen hen clut tai Ichoan. -1 la khach
hang, 1 la admin
Trang 89/109
masach INT MA sach (khoa chinh)
maloai INT MA the Logi
maNXB INT MA nha xuat ban
tensach NVARCHAR 500 Ten ciia sash
tacgia NVARCHAR 500 Tic gia ciut sach
namxuatban INT NAm xuat bin sach
gia MONEY Gia tien
tinhtrang TINYINT T'mh tang can hang (= 1) hoac het hing
mota NVARCHAR 1000 GU% thieu ye cuon sach
linkanh NVARCHAR 500 Dating dan den anh bia cult sach
INT MA boa dan (lchoa chinh)
maKH INT MA kbach hang
tongtien MONEY Tong se tien thanh town
ngay DATETIME Nwly tgo hoa don
hoten NVARCHAR 500 Ho tai Ichtich at mua
diachi NVARCHAR 1000 Dja chi giao hang
sodienthoai INT
tinhtrang TINY1NT Tinh tang clia hem don. Gom co Ddt
mai, Thew xac nhan, Dd thank tocin, va
Hoitn thanh
Chuang 3 - Chuang trInh Cmg dung
Trang 90/109
mabanghi NT MA ban ghi (cheta chinh)
•
magiohang NVARCHAR 100 MA gid hang
masach MONEY MA sich
soluong NT So Itrong sach
makh INT MA khich hang
ngaykhoitao DATETIME Ngity khoi tao
Chuang 3 - Chuang trinh drag dung
3.3. Mai truirng ca d#t
Ng8n ngft lap trinh: ASP.NET
MVC3, chi do Razor View.
ang cv xay dvng: Microsoft Visual Studio 2010 SP1 Deverloper va Microsoft SQL
Server 2012 Deverloper.
Trang 91/109
Chuang 3 - Chuang trinh Crng dung
3.4. MA ngubn chirang trinh
& Controllers
AccountControlletcs
BookController.cs
CategoryController.cs
ca ChedcouKontrolles.cs
HomeAdminControlkr.cs
HomeController.a
OrderControlletcs
OrdeDetailController.cs
PublisherController.cs
j ShoppingCaitController.cs
• & Scripts
iquay-15.1-vsdocis
jquery-15.1js
jquery-15.1.minjs
jquely-ui-1811,js
jquerui-1811.min tis
Cljquery.unobtrusive-ajaajs
jquery.unobtrusive-ajammin.js
jquay.validate-vscloc.js
iquery.valiclatejs
jquery.velidateanin.js
iquely.validattunobtrusivetis
jquery.validate.unobtxusive.minjs
4) Microsoftlijacdebulk
MkrosoftAjacjs
MicrosoftlakcAjacdebug.js
MicrosoftMvcAjax.js
MicrosoftMvcValidation.debug.js
MicrosoftMwValidationjs
3.4.1. (Au Vac calc file trong terng dyng
• al BookStore •
D al Properties
References
App_Data
• Q
Content
0.1018941
althemes
a Sete.css
• & Models
AccountModeLa
BookModel.cs
BookStore.edmx
CategoiyModel.cs
EFReposibary.cs
• OrderDetailModelcs
OrderModelcs
• PublisherModel.cs
ShoPPin9Caitcs
ShoppingCartModetcs
• & Views
Ca Account
D al Book
101 Compri)
D al Checkout
• Home
D CI HomeAdmin
D al Order
D Ca OrderDetail
D al Publisher
D al Shared
D Ca ShoppingCart
ViewStart.cslitml
Web.config
• Global.asa
Globalasax.cs
packages.config
D 5 Web.config
Trang 92/109
Chuang 3 - Chuang trinh frng dnng
3.4.2. doh xa cac 16p v64 file tiro ng Crn
Models
BookStore.edmx Chim ck doi Wang có hung
ca set de lieu: user, loaisach,
nxb, sach, hoadon, hoadonct
vi giohang.
Chita cac phuong thfrc ket
not ten ca so de lieu.
BookAddModel BookModel.cs II ViewModel, the hien cho
cac de lieu Wang ling la
sach, user, loaisach, nxb,
hoadon hay giohang.
BookEditModel
AccountAddModel AccountModel.cs
AccountEditModel
AccountLoginModel
ChangePasswordModel
CategoryAddModel CategoryModel.cs
CategoryEditModel
PubisherAddModel PublisherModel.cs
PubisherEditModel
OrderEditModel OrderModel.cs
SendOrderModel
ShoppingCartAddModel ShoppingCart.cs
ShoppingCartEditModel
EFRepository EFRepository.cs Thao the de lieu doi voi:
user, loaisach, nxb, sack,
hoadon va hoadonct.
ShoppingCart ShoppingCart.cs Thao tic de lieu di viii
giohang.
Trang 93/109
Chuang 3 - Chuang trinh irng dung
Controlle.rs
AccountController AccountController.cs ()win 15, wow tic, Mm tee
voi model va awn ra view de
hien thj.
BookController BookController.cs
CategoryController CategoryController.cs
PublisherController PublisherController.cs
OrderController OrderControllencs
OrderDetailController OrderDetailController.cs
ShoppingCartController ShoppingCartController.cs
CheckoutController CheckoutController.cs
HomeController HomeController.cs
HomeAdminController HomeAdminController.cs
Views
Account Add.cshtml Them ngubi dung
ChangePassword.cshtml Thay di mit khau
ChangePasswordSuccess.cshtml Thong bio thay d6i thanh cong
Info.cshtml Thong tin nguiri dung
List.cshtml Danh sach ngubi dung
Login.cshtml Ding nhap
Register.cshtml Ding kY ngubi diing
ViewEdit.cshtml Giao diem chinh sera ngubi ding
Book Add.cshtml Them sach
BookByCate.cshmtl Hien thj sach theo loci sach
BookByPub.cshtml Hien fill sach theo nha St ban
Detail.cshtml Chi titt ye cam sach
Danh sach sin phtun List.cshtml
Trang 94/109
Chuang 3 - Chuang trinh Cling dung
NewBook.cshtml Hien thj sach mai
RandomBook.cshtml Hien thj sach nau nhien
ViewEdit.cshtml Giao then chinh sera sach
Category Add.cshtml Them nhi xuAt ban
CateMenu.cshtml Hien thi loai sach trong menu
List.cshtml Danh sach loci sash
ViewEdit.cshtml Giao hien chinh sera sach
Home Index.cshtml Trang chii cfia website
About.cshtml Trang gieri thieu ye website
HomeAdmin Index.cshtml Giao dien phan (pin ly
Order List.cshtml Danh sach hoa don
ViewEdit.cshtml Giao dien chinh sera tang thai hea don
ViewOrder.cshtml Giao dien hien thj Ma don cho khich
OderDetail List.cshtml Danh sach Ma don chi tiet
ViewOrderDetail.cshtml Giao dien hien thj Ma don chi tiet
cho kluich
Publisher Add.cshtml Them nha mitit ban
List.cshtml Danh sach nha xuat ban
PubMenu.cshtml Hien thj nha )(at ban trong menu
ViewEdit.cshtml Giao dien chinh sera nha St ban
ShoppingCart Index.cshtml Giao dien at mua sach
Checkout SendCartOffer.cshtml Giao dien nhap thdng tin d6 at hang
Complete.cshtml Giao dien hoin tat dat hang
Shared Chita cac giao dien lam khung cho
website.
Trang 95/109
public void
public nxb
public nxb
public void
InsertPublisher PublisherAddModel
FindPublisherBy
Name
string
FindPublisherBy
ID
int
public
public void InsertCategory
public loaisach FindCategoryBy
Name
public loaisach FindCategoryBy
ID
public void EditCategory
public void DeleteCategory
Kiem tra xem ce x6a
dirge nha xuat ban
khong. Neu khong co
rang buk thi thqc hien
x6a, ngtrqc 1 i till thong
bito khong duqc x6a
Them loci skh vao
CSDL
Tim loci sich theo ten
Tim lo3i sach theo ma
Sira thong tin ve logi Mob
trong CSDL
Kiem tra xem c6 x6a
dugs lo3i sash kh8ng.
EditPublisher PublisherEditModel
an
CategoryAddModel
string
int
CategoryEditModel
int
Tim nha xuat ban theo
ten
Tim nha xuat ban theo
ma
Sin thong tin ve nha t
bin trong CSDL
Them nha xuat ban vio
CSDL
Chuang 3 - Chtrcmg trinh img dung
3.4.3. Sa kyr ye cac l&p chinh
3.4.3.1. Thong Models
Trang 96/109
Chtrcmg 3 - Chtrcmg trinh irng dung
Neu khong co rang buck
thi thuc hien x6a, nguqc
lai thi thong bio kitting
duqc x6a
public void InsertUser AccotmtAddModel Them ngutri dung vio
CSDL
public user FindUserByLogin
Name
string Tun nguiri dimg theo ten
ding nh4p
public user FindUserBylD int Tim ngutri dung theo mA
public bool IsUserExits string, string Kiem tra xem ten ding
nhip eh ton 4i khong.
public bool CheckUserState string Kiem tra xem tang thii
dm ngutri dimg li binh
thuong hay bj khoa
public bool IsAdmin string Kiem tra xem ngtrtri
ding c6 phii li admin
hay khong
public void EditUserStatus AccotmtEditModel Clip nh4t tiling thii cho
ngueri dimg
public void EditUserPassword ChangePassword
Model
Thay del mit khau cho
tai khoin
public bool DeleteUser int Kiem tra xem co xda
duqc ngtrtri dimg khong.
Neu khong co rang butIc
thi thuc hien x6a, nguqc
lai thi thong bio kh8ng
duqc x6a
public BookAdd GetConslrainAdd Ley cic mi nhi mitt bin
Trang 97/109
Chuang 3 - Chuang trinh (mg dung
Model va ma loci dang co trong
he thong de khi them
sach ta co the Iva chqn.
public Bo okAdd
Model
GetConstrainEdit Lay cat ma nhi xuat bin
va ma loci dang co tong
he thong de khi chinh dra
ta c6 the lira chip
public sach FindEioolcByName string Tim sach theo ten
public sach FindBookBylD int Tim sach theo nil
public void InsertBook BookAddModel Them sach vao CSDL
public void EditBook BookEditModel Chinh sin lai each
public bool DeleteBook int Kiem Ira xem th x6a
duqc sach kh8ng. Neu
kitting eh ring buOc thi
thuc hien x6a, nguqc lai
thi thong bao khong dtrqc
x6a
public lEnumerable
GetNewBooks Lay ra danh sach cric
sach mai
public rEnumerable
GetBoolcsByCatt int Lty ra sach theo loaf sach
public ffinumerable
GetBooksByPub int ay ra sach theo nha xuet
ban
public lEnumerable
GetRandomBook
•
Lay 1 danh sach ngiu
nhien ve sach
public hoadon FindOrderBylD int Tim h6a don theo ma
public void EditOrderState int Sim lai tang thai
public void DeleteOrder int X6a mOt hoa don
Trang 98/109
Chuang 3 - Chuang trinh Crng dung
3.4.3.2. Thong Controllers
Lay ra danh sach h6a dcrn
cfm mot khfich hang
Lay ra h6a don chi tiet
cfm mot don hang
public void AddToCart sach Them sach vio gi6 hang
public int UpdateCattCotmt int, int C#p nhat so Itrcmg sach
public int RetnoveFromCart int X6a mot sin phihn trong
gio hang
public void EmptyCart X6a town b0 gio himg
public decimal GetTotal Lay ra tang tien
public List
GetCartltems Lay ra danh sach gio
hang
public void CreateOnlerDetall hoadon Tao Ma dun chi tiet cho
mot don hang
public
public
IEnumerable
GetOrdersByUser
ID
int
int
ActionResult Login
ActionResult Info
ActionResult Register
Phuong thirc get, tra ve
View de ding nhip
Phuong thirc post, xfr
lY so !den ding nliap
Phuong thfic get, thoc
hien viec Sing xuat
Tra ve thong tin nguiri
dimg
Phuang thirc get, tri ve
public
public
ActionResult Logout
AccotmthoginModel
public
public
public
Tsang 99/109
Chuang 3 - Chuang trInh Crng dung
view de ngtroi dung
ding kY tai khoin
public ActionResult Register AccountAddModel Phuong thfrc post, icfr
ly tee thing Icy Si
khoin
public ActionResult List Tra ye danh sash Si
khoan
public ActionResult Add Phuang thtic get, tra ve
view de nguiri quan ly
them ngtdri dug
public ActionResult Add AccountAddModel Phuong thirc post, xir
ly viec them tai khoan
public ActionResult ViewEdit int Tra ye view de quart ly
chinh sira tai khoin
public ActionResult EditStatus AccountEditModel Phuong thtic post, time
hien viec chinh sirs
th8ng tin Si khoin
public ActionResult Delete int Xir ly ilia Si khoin
ngtrai dims
public string Encrypt string nut hien ma hem
chugi du vio
(password)
public ActionResult ChangePassword Phucmg thirc get, tra ve
view de thay di
password
public ActionResult ChangePassword ChnePasswordMcdel Phuong thirc post, zfr
ly viec sfrs password
eta ngtrai dung
Trang 100/109
Chuang 3 - Chuang trInh Cmg dung
Add
ActionResult RandomBook
ActionResult List
BookAddModel Phuang that post, xi:
ly viec them sich
int
Tra ve danh sach san
pliant sach
Tra v view de nguti
quart 15, chinh sua thong
tin sach
ActionResult ViewEdit
public ActionResult
public ActionResult
public ActionResult
public ActionResult
Edit BookEditModel
BookByPub int public ActionResult
public ActionResult
Delete
NewBook
ActionResult public
int
public
public
public
public
Plurcmg that get, tra v
view de ngtrai quan ly
them sich
Lay ra met so luqng
sich ngiu nhien
BookByCate int
Detail
Phuong thirc post, thuc
hien viec chinh situ
th8ng tin sich
Xir ly x6a sach
Lay ra nhang sach mei
Xir ly viec lay ra sach
theo loai sach
Xir ly viec lay ra sach
theo nha xuAt ban
Xir 15, viec xem chi tiet
met coon sich
ActionResult Change Password
Success
Tri ve thong bao they
&di password thinh tong
public
ActionResult Add
Trang 101/109
Chuang 3 - Chuang trInh img dung
public ActionResult
public ActionResult
public ActionResult
public ActionResult CateMenu
CategoryAddModel Phucmg thirc post, xir
it viec them loci sach
ActionResult Delete
int
CategoryEditModel
int
Add
Add
List
public
public
public
Tra ye danh sich loci
sach
Trd ve view de quan
chinh sira thong tin 1*
sach
Pinning thirc post, thuc
hien viec chinh sira
th8ng tin loci sach
Xir ly x6a loci sach
Lay danh sach
sach
m menu
Phucmg thirc get, tra v
view de ngulyi quail 15,
them loci sach
ActionResult ViewEdit
ActionResult Edit
Phuang thtrc get, tra v
view de nguai quim ly
them nha St ban
Add
Add
CategoryAddModel
public
public
public
ActionResult
ActionResult
ActionResult List
Phuong thirc post, 'dr
ly viec them nha xuAt
bin
Tra ve danh sach nhi
xuat ban
Trang 102/109
Chuang 3 - Chuang trinh ling dung
Delete
PubMenu
Edit
int
int ActionResult
ActionResult
ActionResult
ActionResult
ViewEdit
CategoryEditModel
ActionResult RemoveFromCart
List
int public
public
public
ActionResult
ActionResult
ActionResult
ViewEdit
Edit OrderEditModel
Tra ye view de ngubi
quan ly chinh sera thong
tin nha xuit ban
Phut:mg thirc post, thirc
hi8n vi8c chinh sire
thong tin nhi xuat ban
Xfr ly x6a nhi xuat ban
Lay danh sich nha xuat
ban ra menu
AddToCart
UpdateCartCount
int
int, int
int
Tri ve danh sach gib
hang
Xfr ly vi8c them sin
phin' vio gib hang
Xir lY viec clip nhcit so
luong san phim
Xir ly vi8c x6a sin
pan trong gib hang
Tri ve danh sich hea
don
Tri ve view de ngueri
quin ly chinh sira tang
thai hoa don
Phucmg thirc post, thuc
hi8n viec chinh sire
public
public
public
public
public
public
public
public
ActionResult
ActionResult
Trang103/109
Clurcmg 3 - Chuang trinh irng dung
public Complete ActionResult Tra ve thong bao cho
Ichich hang
Index public ActionResult TM ye giao din qua
try cho ngubi quail IS,
public
public
ActionResult
ActionResult
List
ViewOrderDetail int
Tra ye danh sach h6a
don chi tier
Tra ve danh sach Ma
don chi tie't theo ma
h6a don
public
public ActionResult SendCartOffer SendOrderModel
Tra ve view de Ichich
hang nh$p thong tin
lien hg cho viec tao Ma
don
Xir ly viec xac nh4n
th8ng tin lien he' caa
khich hang va tao Ma
don
ActionResult
ActionResult
int
thong tin Ma don
Xir ly x6a Ma don
TM ve Ma don theo
Mich hang
ViewOrder
public
public
Delete
Trang 104/109
Chuang 3 — Chuang trinh Ong dung
public
laiitilfiTi napbsagaill
ActionResul1111111111111111111111111111111111111.111.1111111111ndex
Viatitiii aid* MO 1
giro then tang
chaff cila website
public ActionResuh About Tra ye giso din gifri
Neu dm hang
3.5. bRit so hinh fah mink bta vi frog Aug
Iftnh 3.4Trang chi trio website
Trang 105/109
Chuang 3 - Chuang ninh ang dyng
Thong tin
Tin NM Mit Bin
Thong tin
HIM 3.5 Giao dien Alm moi nha xuat ban
NI NM Tan POW Thong In
1 Kim Deng 55 Quang Thing. Hit NOi
2 Tre S6 21 clay A11 khu gam Tian, Ha NO
3 Lao dOng xa hOi NO MA Binh 4. Minh Khai, Ha Bit flung. Ha NOE
4 Van hem thong tin 43 Lo flik, HE NO
5 Dan hi S6 9, Ngd 26. Hoang cite, song oa, Ha NOi
6 Khoa hcic va II '1141 70 Tran Hung Boo, Ha Nei
7 Quan obi 23 Li Nun Be, Ha Pei
8 CON an nhan din 167 Mai Ilk De, Ha HOE
10 aa Wing 15 Nguyen Chi Thank Da Nkg
11 Dal K 4X (KA Oa Hei NO' 16 Hang Chu& Ha NOE
13 MK nil 39 Hang Chub, Ha Noi
14 Nghe An 37 Le Hang Phong, Vinh, Nghe An
15 NOM nghiOp S6 6, ngd 167 Phuong Mai, Ha NOE
16 Giao chic 81 Din Hung Deo. Ha Nei
17 Van 146a Sal Gin 310 Tan Hung ipao. own 1. Tp. H6 Chi Minh
Trapp 1N «Prav P4.4"
Hinh 3.6 Danh sack cdc nha nit ban trong he tang
szionma
Aga
chit
Qnintuia
geintuita
aintsia Xba
chat sim Atm
Stainithia Xba
iita
ginkgo Ada
Chiruh s&a gag
cotionaim
oat Sin Ma
Ciliontha Ada
lint Ada
Trang 106/109
55 Quang Trung, Ha 11101.
4
Ten NM Xuat Ski Km Deng
Thong tin
Chuang 3 - Chiron trinh Cmg dung
Thong lin
Hinh 3.7 Giao dien chink saa tong tin nha xsuit ban
Tin sash Oth we SS Mame
j am Sep Khong Chi I a KM Tight 65,000 VNO 1 StaLLOgatathEZOQ I glasithhoo
Academic %/madam In I hag 45.000 VIC 2 augalittizon I &utilising
Tong s6 ben (VND) 155000 VIE
Hin ► 3.8 Giao diet: vJ char nem dat hang
Trang 107/109
KgT LUAN
Trong kh6a lujn nay, em de trinh biy nhfing hieu biet ye ASP.NET
MVC Framework,
veri trgng tam la ASP.NET
MVC3. Ben canh dO, em ding da xay dung mOt website dui
tren ding nghe ASP.NE'f MVC.
Tii lieu nay di dun ra nhung van di chinh sau:
- Gioi thieu ve ASP.NET
MVC vi lim quen veri mOt (mg dung ASP.NET
MVC;
- Tim hieu chi tiet ve cic thinh phin clfinh li Controller, View, Model binding,
Model validation, jQuery;
Neu ra mOt st phuong thirc tan ding website vi cich ph6ng chtmg;
M8 ti ve chuang trinh time dung website gied thieu vi dit sich hue tuyen.
}Chem lujn duqc thuc hien trong then gian tir thing 4/2013 den thing 7/2013. Hy yr:mg
veti viec thuc hien de tai nay, em se cung cap mOt Si lieu Tiering Viet cu bin vi dly du ve
ding nghe ASP.NET
MVC, glop ich cho nhOng ngueri mutin tim hitu ve no sau nay.
MOt lan n0a, em xin chin thinh cam an qujr thly co di tio dieu kien gilip der em
hoin thinh kh6a luan nay.
• Kit qui thu dirge
- Nang cao hieu biet ve mOt c8ng ngh'e fien tien.
Cii thien kha ning viet, djch va tra cau Si
- Xay dung mOt img dung veri nhung chic Wing can thiet.
• HuOng phit triers
- Tim hieu nhung kien thirc meri han ve ASP.NET
MVC.
- Tim hieu nhung yeu cAu thuc to phit sinh dOi yeti chuung trinh img dung, qua do
cai thien nhimg chirc ning sao cho tOi uu nhAt co the.
Trang 108/109
TM Lieu TRAM KHAO
Stich
(1) Pro ASP.NET
MVC Framework 3 - Steven Sanderson
Website
(1) htto://weblogs.asp.net
(2)
(3) htto:// 3ww.codeproiect.com
(4)
(5)
Trang 109/109
Các file đính kèm theo tài liệu này:
- khoa_luan_tim_hieu_ve_cong_nghe_asp_net_mvc_framework_cua_mi.pdf