I. ASP.NET MVC3 1. 2. 3. MVC 4. MVC 5. 6. , MVC 7. SportsStore:
8. SportsStore: 9. SportsStore:
1920 34 4469 95 125
151
182225
II. ASP.NET MVC 3 10. MVC 11. URL, 12. 13. 14. 15. 16. 17. 18.
19. Ajax 20. jQuery
265 266283 331 364 390 417 459 487 507
545567
III. ASP.NET MVC 3 21. 22. 23.
595596 613 639 663
17 17 18 18
I. ASP.NET MVC3 1.
-------
20
- ASP.NET Web Forms ASP.NET Web Forms - - REST Ruby on Rails
Sinatra Node.js ASP. NET MVC MVC HTML HTTP ASP.NET A PI- ASP. NET
MVC ASP. NET MVC ASP.NET Web Forms Web Forms MVC Ruby on Rails
MonoRail ASP. NET MVC 3 2. Visual Studio 2010
20 21 22 23 23 24 25 25 26 26 27 27 28 28 29 29 30 3031
31 32 32 3233 33 34 34 34 35 37 38 39 39
- -
40 4243
3. M VC
744 44 46 48 48 49 51 53 53 54 55 57 59 63 66 68 69 69 70 70 71
72 72 76 77 77 78 80 81 82 83 84 85 86 93 94 95 95 95 98 99 104 106
106 107 113 114 116 124
ASP.NET MVC -
4. MVC
MVC MVC ASP.NET- MVC MVC - MVC
5. C# - L1NQ Razor Razor
8
125
6. , M VC
Ninject Ninject Ninject ASP.NET MVC Visual Studio ( ) Moq Moq
Visual Studio Moq Moq Moq
126 127 128 130 131 132 133 134 135 137 137 139 143 144 144 145
145 148 149 150
7. SportsStore: Visual Studio DI Entity Framework Product URL
CSS
151152 1 5215 4
154 156 157 157 1 5815 8 15 9
160 161 162 162 163 163 164 165 167 168 1 69 176 177 1 77 1 78
179 181
8 . S p o rts S to re : U R L Cart
9182182 182 185 186 193 195 196 199 200 202 205 205 209 209 210
212 212 213 216 218 219 222 222 224 225 225 226 227 228 230 233 244
246 248 248 249 251 252 253 256 256 257 258 258 259 260 262 263
9. SportsStore: C R U D Account Entity Framework Getlmage
10
II. A S P . N E T M V C 3
265 266 266 270 270 272 272 273 274 277 280 282 283 283 284 285
287 290 292 295 297 298 300 7. 306 308 309 310 310 316 317 318 318
322 323 323 325 326 328 328 328 330 331 331 331 331 332
10. MVC MVC Visual Studio - - MVC MVC Visual Studio Visual
Studio Edit and Continue
11. URL, URL URL URL URL URL URL URL RouteBase URL URL g e t p o
s t :
12. IController Controller
11
HTML- XML JSON HTTP
334 334 336 338 339 343 346 350 354 355 356 357 360 361 363 364
364 366 366 367 372 376 381 386 389 390 391 391 393 393 394 395 396
398 399 400 403 405 406 408 416417
13. 14. Def aultControllerFactory REST ,
15. IV iew iV iew E n gin e Razor
417 419 420 421 423
12
Razor Razor Razor HTML HTML HTML HTML
423 425 427 428 429 434 434 436 437 450 452 453 453 454 455 457
457 457 458 459 459 463 465 472474
16. HTML- V i e w D a t a . T e m p l a t e l n f o
474 477 478 479 480 481 482 483 485 486 487 487 488 489 490
493496
17. ,
497 498 499 499 500 502 502
13
M o d elB in d er 18. 19. Ajax Ajax MVC Ajax Ajax Ajax Aj Aj
Ajax- Ajax JSON JSON JSON Ajax JSON- 20. jQuery
505 505 506 507 507 509 511 515 516 516 520 524 525 528 529 531
534 536 541 544 545 545 545 548 549 sso 551 551 553 554
555556 558 560 561 562 563 564 566 567 567 569 571 572 574 575
577 579
jQuery jQuery- jQ uery OcHOBbijQueiy jQ u eiy jQ uery jQ u
eiy
14
D O M
579581 583 587 588589
jQ uery-, CSS DOM jQ uery j Query jQ uery UI HajQ ueryUI
590 591 592594
III. ASP.NET MVC 3 21. - HTML- . XSS- HTML Razor JavaScript-
XSS- IP- HttpOnly cookie- CSRF Antiforgery SQL- - MVC Framework
595596 596 598 599 600 601 601 604 606 606 606 607 7 0 608 610
610 611 611 611 611 612 612 613 613 615 616 619 619 621 628 631 636
637 638
22. Windows - ,
URL IP-
2 3 .
15639 639 639 640 641 642 651 654 654 654 655 655 655 657 657
658 661 662 663
bln- Web .conf ig IIS - - , IP-
, , , .
, , .
17
, , , . . .NET.
Microsoft - (Web Platform and Tools), - Microsoft . - , , -.
http://github.com/SteveSanderson.
/ , Microsoft. ( www.brainforce.it) Brain Force (
www.brainforce.com) . Microsoft .NET (Microsoft Certified Solution
Developer for .NET), Microsoft .NET (Microsoft Certified
Application Developer for NET), Microsoft (Microsoft Certified
Professional), . 10 , .
18
Apress . , (Jennifer Blackwell) (Ewan Buckingham) . , (Fabio
Claudio Ferracchiati), , .
, , . , , . , . . , - . , , , , . , , . . : E-mail:WWW: inf 00
will iamspublishing.com http://www.williamspublishing.com
: : : 127055, . , . , . 43, . 1 03150, , / 152
I
ASP.NET 3
ASP.NET MVC - Microsoft. , , , -.
, 0CH0ByASP.NET MVC 3, .
1
A
S .NET MVC - Microsoft, 5 --" (model-view-controller MVC), ,
ASP.NET. ASP.NET Web Forms, -, . , Microsoft ASP.NET MVC, , ,
ASP.NET MVC 3.
- ASP.NET MVC, - , . - Microsoft , , . . 1.1, . 1.1. - Microsoft
*
Common Gateway Interface ( ) (CGI)*
-
-, ( ) SQL
Microsoft Internet Database Connector ( ) () Active Server Pages
( ) (ASP)
1996 .
-
1.
21
. 1.1
2002, 2003 .
ASP.NET Web Forms (- ASP.NET) 1.0/1.1
-
HTML
2005 .
ASP.NET Web Forms 2.0 ASP.NET AJAX ASP.NET Web Forms 3,5 ASP.NET
MVC 1.0 ASP.NET MVC 2.0 ASP.NET Web Forms 4.0
2007 . 2008 .
2009 . 2010 .
2011 .
ASP.NET MVC 3.0
* CGI - , . NCSA (National Center for Supercomputing
Applications ).
ASP.NET Web Forms 2002 ASP.NET . Microsoft, , . 1.1. Web Forms
Microsoft HTTP ( ), HTML ( ), , . ( View State ( )), HTML-, ( , ) .
Web Forms , , -. , - W in d o w s Forms. HTTP; . - , , , , , .
22
I. ASP.NET MVC 3
. 1.1. ASP.NET Web Forms
ASP.NET Web Forms ASP.NET W eb F o rm s , . W e b Forms .
View State. ( V ie w S tate) . -, - , . . , , . , View State , .
. (codeb e h in d ) A SP.N ET HTML . , (, ) (, ) . .
1.
23
HTML-. HTML-, HTML, . ASP.NET 4 HTML- - (Cascading Style Sheets
CSS), , JavaScript. ASP.NET 4, HTML- . . Web Forms HTML HTTP, . ,
HTML-. , -. . ASP.NET , . , . . ASP.NET . 2.0 , , . Ajax 2007 .
Microsoft Web 2.0/AJAX, . , ASP.NET 4, HTML-, .
- Microsoft Web Forms - . A j a x .
- REST - . - , - , - (HTML, CSS, JavaScript ..) - ( ). -. HTTP
REST (Representational State Transfer ), SOAP (, -, ASP.NET). REST
(URI), , ( HTTP), . , PUT
24
I. ASP.NET MVC 3
h t tp ://w w w .e x a m p le .c o m /P r o d u c t s /L a w n m
o w e r
d e l e t e
http://
w w w .e x a m p le .c o m /C u s t o m e r s /A r n o ld - S m
it h .
- HTML-; JSON XML , AJAX, Silverlight . REST , - -, HTTP URL,
ASP.NET Web Forms.
- (agile) . , , , . , , ( ), . (test-driven de velopment TDD)
(behavior-driven development BDD). , ( , ), , . .N E T , T D D / B
D D ,
, Web Forms. . , , . , Web Forms. , , Web Forms. . . Web Forme,
, . , Web Forms HTML , . (independent soft ware vendor ISV) (NUnit
MBUnit), (Rhino Mocks Moq), (Ninject AutoFac), (Cruise Control
TeamCity), -
1.
25
(NHibemate Subsonic) .. , ALT.NET. ASP.NET Web Forms , .
Ruby on Rails 2004 . Ruby on Rails , . , -. , Ruby on Ralls , ,
, , , . Ruby on Rails ( Rails, ) MVC. MVC HTTP, , -
(object-relational map ping ORM) Rails . , -: , , , , , , . Ralls ,
- REST . , , . - .
Sinatra Rails, -, Ruby . Rails . , Sinatra, 2007 . Sinatra Rails
( , , ..) URL Ruby. URL. Ruby, . - , . -, , -, REST, ( REST 14). -,
Sinatra HTML ORM, -, . , Sinatra MVC-, Rails ( ASRNET MVC). , - , ,
Sinatra , .
26
I. ASP.NET MVC 3
Node.js JavaScript . A J A X JavaScript; jQueiy , ; JavaScript-
V 8 Google , . JavaScript . , CouchDB Mongo, , Node.js. Node.js 2 0
0 9 .. . Sinatra , M VC. H T TP - . .
JavaScript. , , CouchDB ..
. API- Node.js - . - , -. , Node.js (, 100 ).
Sinatra, Node.js . , , , Ruby on Rails ASP.NET M V C . Node.js ,
ASP .N E T M V C .
, ASP.NET M V C (00 1 4 ). H T T P - . , , ASP.NET M V C
JavaScript, ( 18, 19 20).
ASP.NET MVC ASP.NET , , - , , . Mieresaft Web Forms, . 2 0 0 7
ALT.NET , . , . Microsoft (Scott Guthrie) - M V C . ASP.NET ,
Ralls, Web Forms. , Web Forms A S P . N E T .
1 .
27
MVC MVC ASP.NET MVC. MVC (model-view-controller --) 1978 .
Smalltalk, Xerox PARC - . MVC : , . . -, HTTP. -, ( , , HTML ), ,
MVC. * ASP.NET MVC MVC . ASP.NET MVC MVC, -. 4. MVC, ASP.NET MVC
Ruby on Ralls , MVC .NET. , , , ASP.NET MVC , Rails.
, , . , , , , , . MVC .NET , , , , . MVC Framework ASP.NET MVC .
( ) , ( ). . .
28
I. ASP.NET MVC 3
ASP.NET 2.0, MVC. , , 10.
HTML HTTP ASP.NET MVC . HTML , Web Forms. HTML-, MVC , CSS . , ,
, ASP.NET MVC " , jQuery Yahoo YUI. JavaScript , , jQuery , ASP.NET
MVC , jQuery ,js CDN (Content Delivery Network ) Microsoft. jQuery
20. ASP.NET MVC View State, ASP.NET Web Forms. , . Ruby on Rails,
ASP.NET MVC g HTTP: , , . AJAX , . , , , .
, , MVC. . ASP.NET MVC . - , . Visual Studio ( , NUnit xUnit,
MSTfest Microsoft). , . , ASP.NET MVC, .
1 .
29
. ASP.NET MVC , . , , , HTML-, CSS , .
- URL-. URL : /App_v2/User/.
aspx?action=show%20prop&prop_id=82742 , , : / to -r e n t/ c h
ic a g o / 2303 - s i l v e r - s t r e e t , URL-. , , URL, . rent
in Chicago ( ) URL. -, - , URL, . -, - URL-, , . -, , , , , , ). U
R L - ,
ASP.NET MVC System .W eb.Routing, URL . URL , URL- . , , URL
REST, . URL 11.
ASP.NET ASP.NET Microsoft , -. , ASP.NET M V C .NET, , .NET.
API-, M V C , -NET, .NET . -, ASP.NET, -, , , , , , , MVC , Web
Forms. ASP.NET
30
I. ASP.NET MVC 3
Web Forms, ASP.NET ( , Web Forms , View State). . ASP.NET Visual
Studio - , - Internet Information Services (IIS), Windows , Windows
Vista, Windows 7 Windows Server. IIS 7, - .NET , AS P. NET.
ASP.NET. MVC . 23 , ASP.NET MVC IIS Windows Server.
API- 2002 . Microsoft .NIST , . AS .N 1ST MVC 3 .NET 4, API- , ,
-, , LINQ (Language Integrated Query ). API- MVC Framework , .
ASP.NET MVC - Microsoft, ASP.NET MVC , . , , ( , ). , , , . , ,
, . . ASP.NET MVC Microsoft (Microsoft Public License Ms-PL; h ttp
:/ / w w w .o p en so u rce.o rg / licen sesy m s-p l.h tm l), ,
(Open Source Initiative OSI). , , . Microsoft , . Microsoft , . MVC
h t t p ://a s p n e t .c o d e p l e x .c o m /.
1.
31
ASP.NET MVC , ASP.NET M V C . M V C . , , MVC, , . . - , , .
ASP.NET Web Forms , ASP.NET Web Forms, , ASP.NET M V C . , Web
Forms . Microsoft , , Web Forms. . . W eb Forms , . HTTP H T M L ,
View State . Windows Forms, ( ]
, . M V C HTTP, , . . M V C -. , - , , . Web Forms , , , , , M V
C . , , , (w iz a r d ). ,
Web Forms, , . , , .', , MVC.
32
I. ASP.NET MVC 3
Web Forms MVC ASP.NET Web Forms, MVC, , . , , Web Forms -. ,
.
Ruby on Rails Ruby on Rails , -. , Microsoft .NET, ASP.NET MVC,
, Python Ruby Linux Mac OS X, Rails. , Rails ASP.NET ' MVC . .
Rails , .. , , ORM . , ASP.NET MVC - MVC . ORM, . . .NET , . , ORM,
NHibernate, Subsonic, M i c r o s o f t E n t i t y F r a m e w o r
k . .NET, , ASP.NET MVC. Rails.
MonoRailMonoRail MVC- - NET, i Castle , 2003 . MonoRail ASP.NET
MVC
MonoRail , Rails- ASP.NET, , Microsoft. MonoRail . , ( -
.NET
, . ( ASP.NET MVC MonoRail . - .NET ASP.NE1 MVC.
1.
33
ASP.NET MVC 3 MVC 3 Razor (Razor View Engine). MVC ASP.NET,
ASP.NET, , , ). Razor , . Razor . - , Microsoft , Razor MVC. . ,
Razor . Razor MVC 3. Visual Studio . JSON JavaScript, jQueiy.
, CGI , , . , ASP.NET Web Forms - Microsoft 2002 ., -, Microsoft
- . , ASP.NET MVC ASP.NET Web Forms, , , . MVC , , . 7 , , , .
2 . 4 .49 0
2
, MVC, . , , , , .
MVC . Visual Studio 2010, - (Web Platform Installer) , IIS
Express. , .
Visual Studio 2010 MVC Visual Studio 2010. Visual Studio
Microsoft (IDE), , , , , - M icrosoft.M ic ro s o ft V is u a l S
tu d io 2 0 1 0 ,
. : Visual Studio 2010 Professional Visual Studio 2010 Premium
Visual Studio 2010 Ultimate , . Visual Studio Windows- , .
V is u a l W e b D e v e lo p e r E x p r e s s M icrosoft
Visual Studio, Express. - V is u a l W e b D e v e lo p e r 2010
Express. . , MVC, , , , Visual Studio.
2.
35
Web Developer Express MVC, , NUnit ( http://www.nunit.org) .
NUnit , , Web Developer Express . , MVC , Web Developer Express, ,
. Visual Studio 20 10 Ultimate Edition 7.8 , . , , , . 2.1.
. 2.1. Visual Studio Visual C# Visual W eb Developer. Visual
Studio S Q L Server 2 0 0 8 Express, , , . , , Visual Studio , , ,
, . 2.1, , M VC. Visual Studio, .NET 4 , , . ! Visual Web Developer
Visual Studio 2010 MVC 2. . , MVC 3.
Microsoft . , Visual Studio 2 0 1 0 M V C 2.
36
I. ASP.NET MVC 3
( MVC 3) - (Web Platform Installer WebPI). WebPI , Microsoft, -
Microsoft. , . WebPI, http://microsoft.com/we)/clownloacls , . 2.2.
( Microsoft , - .)
. 2.2. - . ; WebPI. Windows, WebPI. -. WebPI , , . 2.3.
. 2 .3 . Web Platform I s a l r ntle
2.
37
. Products (). , , : Visual Studio 2010 SP1 SQL Server Express
2008 R2 ASP.NET MVC 3 Tools Update ( ASF2 E T MVC 3) .N Add ().
Install (), . !
WebPI SQL Server Express , . , , WebPI .
, : MVC Framework, IIS Express SQL Server 2008 Management Studio
Express. , .
MVC FrameworkMicrosoft MVC Framework . MVC Framework, , , . MVC
h ttp :/ / a s p n e t. " d e p le x .c o m . , Microsoft , , ,
.
IIS ExpressVisual Studio -, MVC Framework. , . , , ASP.NET
Development Server ( ASP.NET), , IIS. , 5SL (Secure Sockets Layer
). , IIS, Visual Studio, ASP.NET Development Server. IIS Express, ,
, IIS. IIS Express .. , . IIS Express , : Visual Studio 2010
Service Pack 1.
38
I. ASP.NET MVC 3
SQL Server 2008 R2 Management Studio Express , , Visual Studio,
SQL Server. SQL Server 2008 Management Studio WebPI.
MVC. , . MVC Framework IIS. Microsoft.
. MVC, , 23.
IIS Windows, , Windows Vista Windows 7. MVC . Windows , -, .
Windows Server. Windows Server 2008 R2, IIS 7.5, . ! MVC 3 IIS 6,
Windows Server 2003 2003 R2. IIS 6 , h t t p ; //haacxea.com /
a rch ive/ 2 0 0 8 / ll /26/asp. n e t-m v e -o n -iis -6 -w a
lk th ro u g h , a spx. , , MVC Framework: Web Server (-), .NET F
ra m e w o rk 4 W eb D e p lo y m e n t Tool, - -
. , Windows Server. Windows Server IIS IIS (IIS Learning Center)
h t t p : / / w w w . i i s . n e t , .
___________________ __________________ , . ASP.NET . . , . ,
ASP.NET 4. MVC 3 Framework , . , 23
2.
39
- , - (IIS) m e Windows Server. Server Manager ( ) 5 Add Roles (
) Roles Summary ( ), . 2.4.
. 2.4. Windows Server Add Roles W izard ( ). ( Windows Server
2008 R2). W eb S erver (IIS) Next (). , , . Web Server (IIS). , :
ASP.NET ( A pplication D evelopm ent ( )): M anagem ent S ervice (
) ( M an ag em en t Tools ( )).
ASP.NET . MVC. Management Service Web Deployment (-), . , . .
Install. IIS, . URL- (h tt p :/ / lo c a lh o s t ), (h t t p
\//MoPi_windows_cepBep). , IIS 7.5, . 2.5.
WebPI . : .N E T F ra m e w o rk 4
Web Deployment Tool 2.0 MVC Framework .NET 4, . - 23. W e b
Deployment Tool MVC. , .
40
I. ASP.NET MVC 3
. 2.5. IIS SQL Server Express 2008 R2. SQL Server Express .
Mixed Mode Authentication ( ) sa. . , MVC Framework , , . , . 23. !
WebPI , . , AS P .N E T M VC, .N E T 4 . SQL Server , IIS. , 23 , .
SQL Server.
- 23 , Wet) Deployment M V C Framework . , IIS . ! Web
Deployment , MVC Framework . . http://learn.iis .net/,
aspx/984/configure-web-deploy.
2.
41
Internet Information Service (IIS) Manager ( ), Start (). . . .
2.6, WIN-2008R2.
. 2.6 . IIS Manager
Management Service Delegation ( j -), , Management () (. . 2.6).
Edit Feature Settings ) , Allow administrators cypass rules ( ) , .
2.7. , .
. 2 .7 . Management Service Delegation
42
I. ASP.NET MVC 3
Back (), . Management Service ( ). Enable remote connections (
), Start (), . 2.8.
. 2 .8 . Management Service ! Management Service
. , S e rvice s () S ta rtu p ( ) W eb M an ag em en t S ervice
A uto m atic () A uto m atic (Delayed) ( ( )).
Web Deployment, URL:https://:8172/Ms D eploy .axd
Web Deployment ( h ttp s , h ttp ) 8172. , , , . ( , , ,
8172.)
, . , , . Microsoft (Microsoft Developer Network MSDN). .NET-,
ASP.NET MVC Framework. ASP.NET , MVC .
2.
43
MVC Framework. MVC Framework, CodePlex http://
aspnet.codeplex.com. , MVC Framework , , . - ASP.NET. - ASP.NET
http://www.asp.net. . , , Microsoft. - IIS. IIS . MVC Framework.
IIS, http://www.ils.net. jQuery. , MVC Framework JavaScript jQuery.
jQ u eiy , http://jQuery.com http://jQueryUI.com. 1 (.') s t a c k
o v e r f l o w . c o m . - http://stackoverflow.com, , , MVC
Framework.
. , MVC 3. 23 Framework .
3
. , ASP.NET MVC Framework. , , ASP.NET MVC. . , . - , , .
ASP.NET MVC Visual Studio. New Project ( ) File (), New Project
( ). Web, , 3 ASP.NET MVC 3 Web Application ( AS P. NET 3), .
3.1.
. 3 .1 . MVC 3 Visual Studio
3.
45
! MVC 3 MVC 2,
. , , P a r t y l n v i t e s . . , . 3.2, MVC.
. 3 .2 . MVC 3
Empty () , , MVC 3. Internet Application - ) , . , . Intranet
Application 7-) Internet Application, , /Active Directory. . Empty,
Use HTML5 semantic markup ( HTML5) . . . ! . 3.2 , . 1, MVC 3 ,
Razor, . . , ASP.NET ( ASPX), . Visual Studio , Solution Explorer (
=) . MVC 3. , Start Debugging ( ) Debug () ( . ) . . 3.3.
46
I. ASP.NET MVC 3
, , . 404 N o t F o u n d ( ).
. 3.3.
, , , Visual Studio Stop Debugging ( ) Debug.
MVC . ASP.NET MVC C# ( S y s t e m . W e b . M v c . C o n t r o
l l e r ). . . - URL . MVC C o n t r o l l e r s , Visual Studio .
MVC , , , . , C o n t r o l l e r s Solution Explorer Visual Studio
Add (), Controller (), . 3.4.
. 3 .4 . MVC
3.
47
Add Controller ( ) SomeCo n t r o l l e r Controller name: ( :),
. 3.5. : , , Controller.
Scaffolding options ( ) , . , , Template () Empty controller (
), . 3.5. ! Add Controller , . 3.5, , , 3 Tools Update ( 3). 2. Add
(), . Visual Studio C o n t r o l l e r #- HomeController.cs -. ,
HomeController System.Web.Mvc.Controller. , 3.1.
3.1. H o m e C o n tr o lle rusing System.W e b .Mvc; r.anespace
Partylnvites .Controllers { public class HomeController :
Controller {p u b l i c string I n d e x () {
return "Hello, world";
} 1
, M VC. Index,
48
I. ASP.NET MVC 3
"Hello, world". , Start D ebugging Debug Visual Studio. Index, .
3.6.
. 3.6.
, , , MVC ASP.NET, , URL- . Visual Studio MVC, , . URL-, Index
HomeController: / / /Home/Index
, http: //-/ h t t p : //_/ , Index HomeController. "Hello,
world. MVC. HomeController, MVC. . Visual Studio , . ~ , U R L - .
, . . . . , G lobal.asax.cs. 7 , , 11 , .
- HTML-, "Hello, world". HTML- , .
3. MVC
49
, In dex, 3.2. 3.2. using System.Web.Mvc; namespace
Partylnvites.Controllers { public class HomeController : Controller
{ public ViewResult Ind e x ( { ) return V i e w () ;
} )
3.2 . V ie w R e s u lt , MVC, . V iew R es u lt View . MVC, . ,
, MVC , . 3.7.
. 3 .7 . MVC . , M V C , ,
. MVC: . Index, , . 3.7, MVC Views .
50
I. ASP.NET MVC 3
, H o m e C o n t r o lle r .c s ( , ) Add View ( ). Add View (
), . 3.8.
. 3 .8 . Add View
Use a layout or m aster page ( -). , 5. Add () V i s u a l S t u
d i o I n d e x . c s h t m l Views/Home. , . 3.7, , .. .c s h tm l
#, Razor.
ASPX, .aspx. I n d e x . c s h t m l . HTML-. , : @{Layout = n u
l l ;
} Razor. . Razor, - . Razor. I n d e x . c s h t m l . 3.3 .
3. MVC 3 .3 . HTML-
51
Layout = n u l l ; < !30CTYPE htm l>
< title > In d e x < / title > < 'head>
ciody> Hello, world (from the view) < /div> <
'html>
. Start Debugging Debug, . , . 3.9. . 3 .9 . Ir.dex, . , M VC ,
. , In d e x V ie w R e s u lt , M V C, H T M L- . M VC, , . 3 ,
-----/ V ie w s / H o m e / In d e x .c s h t m l. V ie w R e s u
lt . , R e d i r e c t R e s u l t , U R L . H t t p U n a u t h o
r iz e d R e s u lt . .
, A c t i o n R e s u l t . . .
, - . M VC , H T M L -
. . V iew B ag . C o n t r o l l e r . V iew B ag , , . 3 3 .4
.
52
I ASP.NET MVC 3 .
3 .4 . using System; using System.Web.Mvc; namespace
Partylnvites.Controllers { public class HomeController : Controller
{ public ViewResult Index ( { ) int hour = DateTime.Now.Hour; V iew
B a g .Greeting = hour < 12 ? "Good morning" : "Good afternoon";
return V i e w ();
} } }
, , . (. 3.5). 3.5. V ie w B a g. - : Layout = null;
} Index
@ViewBag.Greeting, world (from the view)
3.5 Razor, , G r e e t in g ViewBag. G r e e t in g . , . , .. ,
Razor . 0, #. Razor. .
, MVC, . 3.10.
. 3.10. , MVC
3. MVC
53
MVC, . . MVC , , " . .
, -, . : , : , (repondez sil vous plait RSVP); RSVP, ; RSVP .
MVC, , . , , HTML, , 3.6.
3.6. (Layout = null;
C D O C T Y P E html>
< t it le > ln d e x < / t it le >
SViewBag.Greeting, world (from the view)
w e're go in g to have an e x c it in g p a rty .< b r />
(To do: s e l l i t b e t t e r . Add p ic tu re s or som ething.)
(To do: sell it b e t ter. A d d p i c t u r e s or something.) SHt
m l . ctionLink("RSVP Now", "RsvpForm") A
< /htm l>
H tm l.A c tio n L in k HTML. MVC Framework , HTML, , , . A c
tio n L in k : , , , , . HTML 15 16. . 3.12.
. 3 .1 2 .
, , http://sam-cepBep/Home/RsvpForm. H tm l.A c tio n L in k
URL- , /Home/RsvpForm URL RsvpForm H o m eC on troller. , ASP.NET,
URL- MVC . URL, MVC Framework ASP.NET URL .
56
I. ASP.NET MVC 3
, 404 N ot Found. , , URL / H o m e / R s v p F o r m . . R s v
p F o r m H o m e C o n t r o l l e r (. 3.9).
3.9. using System;u s i n g S y s t e m .W e b .M v c ; nam
espace P a r t y l n v i t e s . C o n t r o l l e r s {
p u b lic c la s s HomeController : C o n t r o l le r {p u b
lic V ie w R e su lt I n d e x () {
in t hour = D a te T im e . N ow . H o u r; V i e w D a t a [" g
r e e t i n g " ] = h o u r < 12 r e t u r n V ie w ( ) ; ? "G o
o d m o r n i n g " : "G o o d a f t e r n o o n " ;
}public ViewResult RsvpForm( { ) return V i e w Q ;
} }
R sv p F o r m , . , , ( G u e s t R e s p o n s e ), MVC , . !
- , ,
MVC
.
G u e s t R e s p o n s e , , m v c . , Build Solution ( ) Build
( ) Visual Studio.
, R s v p F o r m Add View ( ). Add View ( ) Create a
Strongly-typed view ( ) GuestResponse. Use a layout or master page
( -) , View engine ( ) Razor, Scaffold template ( ) Empty (), .
3.13. Add, . Visual Studio RvspForm . c s h tm l. H T M L -, Razor-
0model. , .
3.
57
. 3 .1 3 .
, , R s v p F o r m . c s h t m l , HTML- G u e s t R e s p o n
s e . 3.10.
3.10. -.odel P a r t y l n v i t e s .M o d e l s .G u e s t R e
s p o n s e
?Layout = null;
RsvpForm
< /head>
fusing (Html.BeginForm ()) ( Your name: @Ht m l .TextBoxFor (x
=> x.Name) Your email: SHtml.TextBoxFor(x => x.Email) Your
phone: SHtml.TextBoxFor (x => x.Phone)Will you attend? @ H t m l
.D r o p D o w n L i s t F o r ( x = > x . W i l l A t t e n d ,
r.ew s e l e c t L i s t l t e m ( ) { T e x t = "No, I c a n ' t c
ome", new[] {
new S e l e c t L i s t l t e m () { T e x t = "Yes, I ' l l b e
th e r e " , V a l u e = b o o l .T r u e S t r i n g } ,V a l u e
= b o o l .F a l s e S t r i n g } }, " C h o o s e a n o p t i o n
" )
} B i d s { get; set; ) } pub l i c class B i d ( p u b l i c M
e m b e r M e m b e r { get; set; } p u b l i c D a t e T i m e D a
t e P l a c e d { get; set; } p u b l i c d e c i m a l B i d A m o
u n t { get; set; } }
80
I. ASP.NET MVC 3
, Bid Member. . , Bid ( , , ). , #. , . ( ) , . , , , , . , , ,
. , SQL Server, ORM. , .
. , . . . , , , , , . . ( ). , , , , , . . , . : Member, Item (
, Bid , Item). 4.2.
4.2. C# M em ber It e m public class MembersRepository { public
void AddMember(Member member) { /* */ } public Member
FetchByLoginName (string loginName) { /* */ } public void
SubmitChanges ( { /* */ } )
}
4. public class ItemsRepository { public void A d d l t e m
(Item item) { /* */ } public Item FetchBylD(int itemID) { /* */ }
public IList Listltems(int pageSize,int pagelndex) ( /* */ ) public
void SubmitChanges( { /* */ } )
81
} , . . , , . 7 MVC, Entity Framework .
, MVC , . , , . . , . . MyEmailSender, , , , , IEmailSender. , ,
P a s s w o r d R e s e t H e l p e r , , . . 4.7, P a
sswordResetHelper M y E m a i l S e n d e r .
. 4 .7 .
IEmailSender, - PasswordResetHelper MyEmailSender. M y E
mailSender . ! .
, , , , . , ASP.NET MVC .
82
I ASP.NET 3 .
, - C# , . , . , 4.3.
4.3. public class PasswordResetHelper { public void
ResetFassword( { ) IEmailSender mySender = new MyEmailSender();
...call interface methods to configure e-mail details. . .
mySender.SendEmail();
} } . P a s s w o r d R e s e t H e l p e r IEmailSender, , , M
y E m a i l S e n d e r . . PasswordResetHelper IEmailSender,
MyEmailSender. . 4.8.
. 4.8. , , . (dependency injection DI) (inversion of control
IoC). DI , , IEmai l S e n d e r . DI , , , , MVC. DI . Pas s w o r
d R e s e t H e l p e r . , 4.4.
4.4. P a s s w o r d R e s e t H e l p e rpublic class
PasswordResetHelper { private IEmailSender emailSender;
4. MVC public PasswordResetHelper(IEmailSender emailSenderParam)
emailSender = emailSenderParam; (
83
) public void ResetPassword( { ) ... .. .
emailSender.SendEmail(); } } PasswordResetHelper MyEmailSender.
PasswordResetHelper , IEmailSender, , , . PasswordResetHelper . ,
IEmailSender, PasswordResetHelper . PasswordResetHelper , , . !
PasswordResetHelper ,
. . . , , . . , .
MVC DI. AdminController, MembersRepository AdminController
MembersRepository. , IMembersRepository MembersRepository, ,
4.5.
4.5. IM e m b e rs R e p o s ito rypublic interface
IMembersRepository { void AddMember(Member member); Member
FetchByLoginName(string loginName); void SubmitChanges (); } public
class MembersRepository : IMembersRepository { public void
AddMember(Member member) { /* */ ) public Member
FetchByLoginName(string loginName) ( /* */ } public void
SubmitChanges ( ( /* */ } ) }
84
I. ASP.NET MVC 3
, IMembersRepository, 4.6.
4.6. A d m in C o n t r o lle rpublic class AdminController :
Controller { IMembersRepository membersRepository; public
AdminController(IMembersRepository repositoryParam)
membersRepository = repositoryParam; } public ActionResult
ChangeLoginName (string oldLoginParam, string newLoginParam) (
Member member = membersRepository.FetchByLoginName(oldLoginParam);
me m b e r .LoginName = newLoginParam;
membersRepository.SubmitChanges(); // ... - } } & A d m i n C o
n t r o l l e r I M e m b e r s Repository . , A d m i nController
, , . {
, : . : , ? DI, 1. , , , PasswordResetHelper, , MyEmailSender.
DI , , , . , I E mailSender , M y E m a i l S e n d e r ,
IEmailSender. , I E m a i l S e n d e r , , PasswordResetHelper, DI
, MyEmailSender. DI . . Ninject, www.ninject.org. 6. . Microsoft DI
Unity. Ninject, MVC. Unity unity.codeplex.com.
4. MVC
85
DI , . DI, Ninject, . . , (, ), . , M yEmailSender
INetworkTransport, DI , MyEmailSender IEmailSender. . , ? DI , , (
), ( ), , HTTP-, . . , INetworkTransport serverName, DI. , , - , ..
, DI. C# .NET . DI MVC, , Ninject.
ASP.NET MVC Framework , , . ASP.NET MVC , Visual Studio . . - .
, ( ) . , , , - . -. , , , - .
86
I ASP.NET MVC 3 .
, , , - . , , . .
.NET Visual Studio. , MVC ^ In t e r n e t A p p lic a t io n [
- } . #, , , . .
! , 6. , . 4.7 , AdminContoller.ChangeLoginName, 4.6.
4.7. [TestClass] public class AdrainControllerTest {
[TestMethod] public void CanChangeLoginName ( { ) // - Member bob =
new Member ( { LoginName = "Bob" }; ) FakeMembersRepository
repositoryParam = new FakeMembersRepository();
repositoryParam.Members.Add(bob); AdminController target = new
AdminController ( r e p o s i t o r y P a r a m ) ; string
oldLoginParam = b o b .LoginName; string newLoginParam =
"Anastasia"; / / - target.ChangeLoginName(oldLoginParam,
newLoginParam); // - Ass e r t .AreEqual(newLoginParam, b o b
.LoginName); Ass e r t .IsT r u e
(repositoryParam.DidSubmitChanges);
}private class FakeMembersRepository : IMembersRepository (
public List Members = new List(); public bool DidSubmitChanges =
false; public void AddMember(Member member) { throw new
NotlmplementedException ();
}public Member FetchByLoginName(string loginName) { return M
embers.First(m => m.LoginName == loginName);
}
4. MVCpublic v o i d S u b m i t C h a n g e s () {
87
DidSubm itChanges = t ru e ;
} } } C a n C h a n g e L o g i n N a m e . , T e s t M e t h o
d , , A d m i n C o n t r o l l e r T e s t T e s t C l a s s .
Visual Studio . C a n C h a n g e L o g i n N a m e , //
(arrange/act/assert //). ^ , - , , , . , , . , 4.7, I M e m b e r s
R e p o s i t o r y , , . B o b , . M e m b e r . , A d m i n C o n
t r o l l e r . C h a n g e L o g i n N a m e . . , , A s s e r t .
. T e s t () Visual Studio. , . 4.9.
. 4.9.
- A s s e r t , Test Results ( ) . . ! , DI .
. DI, . , , - . , , , , .
88
I. ASP.NET MVC 3
. , FakeMembersRepository, . 6.
"--" (test-driven development TDD), . , , , . , --
(ged-green-refactor). . 1. , . 2. , . . 3. . 4. , . 5. , . 6. . , ,
.. 7. , , . . , . , , , . - Item, 4.8.
4.8. - Itemusing System; using System.Collections.Generic;
namespace TheMVCPattern.Models ( public class Item {p u b l i c int
I t e m I D{
g et; p r i v a t e set;
}
//
public string Title { get; set; } public string Description {
get; set; }public DateTime AuctionEndDate public IList Bids { get;
{ get; set; } } p r i v a t e set;
public void AddBid(Member memberParam, decimal amountParam)
throw new NotlmplementedException();
{
} } }
4. MVC
89
, A d d B i d , , , , . TDD . , . - . . . , , 4.9.
4.9. [ T e s t M e t h o d ()] public void C a n A d d B i d O {
// Item t a r g e t = n e w Item(); M e m b e r m e m b e r P a r a
m = n e w M e m b e r (); Decimal a m o u n t P a r a m = 150M; //
- target.AddBid(memberParam, amountParam); // - A s s e r t .A r e
E q u a l (1, t a r g e t .B i d s .C o un t ()); A s s e r t . A r
e E q u a l ( a m o u n t P a r a m , t a r g e t .B i d s [0].B i
d A m o u n t ) ; } [ T e s t M e t h o d ()] [ E x p e c t e d E x
c e p t i o n ( t y p e o f ( I n v a l i d O p e r a t i o n E x c
e p t i o n ) )] p u b l i c v o i d C a n n o t A d d L o w e r B
i d () ( // Item t a r g e t = n e w I t e m ( ) ; M e m b e r m e
m b e r P a r a m = n e w M e m b e r (); D ecimal a m o u n t P a
r a m = 150M; // t a r g e t .A d d B i d ( m e m b e r P a r a m ,
a m o u n t P a r a m ) ; t a r g e t .A d d B i d ( m e m b e r P
a r a m , a m o u n t P a r a m - 10); } [ T e s t M e t h o d ()]
p u b l i c v o i d C a n A d d H i g h e r B i d () { // Item ta r
g e t = n e w I t e m O ;Member f ir s t M e m b e r new Member ( )
;
M e m b e r s e c o n d M e m b e r = n e w M e m b e r (); D
ecimal a m o u n t P a r a m = 150M; // target.AddBid(firstMember,
amountParam); t a r g e t . A d d B i d ( s e c o n d M e m b e r ,
a m o u n t P a r a m + 10); // A s s e r t . A r e E q u a l (2, t
a r g e t .B i d s .C o u n t ()); A s s e r t . A r e E q u a l (
a m o u n t P a r a m + 10, t a r g e t .B i d s [1].B i d A m o u
n t ) ; }
90
I. ASP.NET MVC 3
, . "//, , . C annotAddLowerBid , , ExpectedException. ! , ,
CannotAddLowerBid, AddBid. , , ,
System.InvalidOperationException. .
, , . 4.10.
. 4.10. A d d B i d (. 4.10).
4.10. A d d B idusing System; using System.Collections.Generic;
namespace TheMVCPattern.Models { public class Item { public int
ItemID { get; private set; } II public string Title { get; set; }
public string Description { get; set; } public DateTime
AuctionEndDate { get; set; } public IList Bids { get; set; } public
Item() { Bids = new List();
}public void AddBid(Member memberParam, decimal amountParam)
Bids .Add (new Bid ( { ) BidAmount = amountParam, DatePlaced =
DateTime.Now, Member = memberParam {
}) ; } } }
4. MVC
91
Item AddBid. , Item Bid. , . 4.11.
. 4 .1 1 .
. , CannotAddLowerBid. , . , , 4.11.
4.11. A d d B idusing System; using
System.Collections.Generic;using System.Linq;
namespace TheMVCPattern. Models { public class Item { public int
ItemID { get; private set; } // public string Title { get; set; }
public string Description { get; set; } public DateTime
AuctionEndDate { get; set; } public IList Bids { get; set; } public
Item() { Bids = new List(); } public void AddBid(Member
memberParam, decimal amountParam) {if (Bids.Count() == 0 | |
amountParam > Bids.Max(e => e . BidAmount)) {
Bids.Add(new B i d ( { ) BidAmount = amountParam, DatePlaced =
DateTime.Now, Member = memberParam }) ;} else { throw new
InvalidOperationException("Bid amount too low");
} } } }
92
I. ASP.NET MVC 3
, , , . In v a l i d O p e r a t i o n E x c e p t i o n , .
! LINQ(Language Integrated Query ). , LINQ -, ( =>). C#
5.
A d d B i d . . 4.12.
. 4.12. , , . , , , , * . , . TDD. , , , , , . , . , , , - .
, , . , , : , . . , . - . , -, .
4.
93
, , . ASP.NET MVC , , , . Microsoft , , , . MVC . .
- . - , , . " , .NET. Selenium RC (h t t p : / / s e l e n i u m
h q . o r g / ), Java, Internet Explorer, Firefox, Safari Opera,
.NET, Python, Ruby , . Selenium ; Java. WatiN (h t t p : / / w a t
i n . s o u r c e f o r g e . n e t / ) .NIST, Internet Explorer
Firefox. API , Selenium, , ( ). . , , , . , . - , , JavaScript , ,
. . . . , . , , . , . , , , . , -
94
I. ASP.NET MVC 3
, , . . , , , . ASP.NET MVC Framework , , , , MVC. , , -,
MVC.
MVC , . . (DI), , . , DI . TDD , . , , .
5
C # , , , . #, M V C . . C # L IN Q C# 2010: ( , 2 0 1 0 .)
LINQ:
C# 2010 ( , 2 0 1 1 .). C # . , Razor , M V C 3. Razor A S P X ,
M V C A S P .N E T W e b Forms. Razor A S P X , - M V C 3, Razor. ,
, Razor, , .
C# #, , , . , , Console Application ( ) Visual Studio. System
.Console .
C # , . 5.1 Product.
96
I. A S P .N E T M VC 3
5.1. p u b l i c c l a s s Product { p r i v a t e s t r i n g
name;p u b l i c s t r i n g Name { g e t { r e t u r n nam e; } s
e t { name = v a l u e ; }
}
, N a m e , . g e t ( (getter)) , s e t ( v a l u e ) . , (.
5.2).
5 .2 . using System;
c l a s s Program { s t a t i c v o i d M a i n (s t r i n g [ ]
a r g s )
{
// P r o d u c t Product myProduct = new Product 0 ; // m y P r
o d u c t . Name = " K a y a k " ;//
s t r i n g p r o d u c t N a m e = m y P r o d u c t .N a m e ;
C o n s o l e .W r i t e L i n e (" P r o d u c t n a m e : {0}", p
r o d u c t N a m e ) ;
} } , . , get set , , . , , , (get) (set) . , 5.3.
5 .3 . p u b l i c c l a s s Product { p r i v a t e i n t
productID; p r i v a t e s t r i n g name; private strin g
description; p r i v a t e decimal p r i c e ; p riva te s trin g
category; p u b l i c i n t ProductID { g e t { return productID; }
set { productID = valu e; }
}
5. p u b l i c s t r i n g Name { g e t { r e t u r n name; } s
e t { name = v a l u e ; } } public strin g Description { g e t {
return d e s c r ip t io n ; } set { description = value; } } . . .
. . . . . }
97
, . , . (. 5.4).
5.4. p u b lic c la s s P ro d u c t { p u b l i c i n t
ProductID { g e t ; s e t ; } p u b l i c s t r i n g Name { g e t
; s e t ; } public strin g Description { get; set; } p u b l i c
decimal P r i c e { g e t ; s e t ; } p u blic s t r i n g Category
{ s e t ; g e t; } } . - , . - , , . C# . . , 5.2, - . , , , . - ,
. , , 5.5.
5.5. p u b l i c c l a s s P r o du c t {p r iv a te s t r i n g
;
p u b l i c i n t ProductID { g e t ;
set;
}
p u b l i c s t r i n g Name { g e t { r e t u r n P r o d u c t
ID + n a m e ;} s e t { name = v a l u e ; }
} public strin g Description { get; set; } p u b l i c decimal P
r i c e { g e t ; s e t ; } pu blic s t r i n g Category { s e t ;
g e t; } }
98
I. ASP.N ET MVC 3
! , , . C# .
, 5.6.
5.6. u s i n g System;class P r o g r a m { static v o i d M a i
n ( s t r i n g [] args)
{
// P r od u c t P r o du c t myProduct = new P r o d u c t ( ) ;
* // m y P r o d u c t . P r o d u c t I D = 100; m y P r o d u c t
. Name = " K a y a k " ; m y P r o d u c t . D e s c r i p t i o n
= "A b o a t f o r one p e r s o n " ; m y P r o d u c t . P r i c
e = 275M; myProduct. C ateg or y = " W a t e r s p o r t s " ; //
ProcessProduct(myProduct); } p r i v a t e s t a t i c v o i d P r
o c e s s P r o d u c t ( P r o d u c t prodParam )/ / . . . -
{
}
P r o d u c t P r o c e s s P r o d u c t , : , , . , , ,
5.7.
5.7. c l a s s Program { s t a t i c v o i d M a i n ( s t r i n
g [] args) {
// P r o d u c t P r o c e s s P r o d u c t (new P r o d u c t
{ P r o d u c t I D = 100, Name = " Ka y a k " , D e s c r i p t i
o n = "A b o a t f o r one p e r s o n " , P r i c e = 275M, C a t
e g o r y = " W a t e r s p o r t s " }) ; } p r i v a t e s t a t
i c v o i d P r o c e s s P r o d u c t ( P r o d u c t prodParam)/
/ ... -
{
}
)
5.
99
, ( { } } , P r o d u c t , . . P r o d u c t , P r o c e s s P
r o d u c t .. P r o d u c t . , 5.8.
5.8. using S y s t e m . C o l l e c t i o n s . G en eri c; c l
a s s Program { s t a t i c v o id M a in (s t r in g [ ] args) s t
r i n g [] { "orange", { 10, 20, " pl um" 30, };
stringArray = { "apple",
L i s t < i n t > i n t L i s t = new L i s t < i n t
>
40 } ; int> {
D i c t i o n a r y < s t r i n g , i n t > myDi ct = new
D i c t i o n a r y < s t r i n g , { " a p p l e " , 10 } , { "
g r a n g e " , 20 } , { " pl um" , 30 }}; } }
5.8 . C# , .
, , , . 5.9 S h o p p i ng C a r t , P r o d u c t s.
5.9. S h o p p in g C a rtusing S y s t e m . C o l l e c t i o
n s . Ge n er ic ; p u b l i c c l a s s S h o pp i n g C a r t { p
u b l i c L i s t < P r o d u c t > P r o du c t s { g e t
;}
set;
}
S h o p p i n g C a r t , L i s t P r o d u c t ( ). , P r o d u
c t S h o p p i n g C a r t , , , - , . , , 5.10.
100
I. ASP.NET MVC 3
5 .1 0 .
p u b l i c s t a t i c c l a s s MyExt ensi onMet hods { p u b
l i c s t a t i c d e c i m a l T o t a l P r i c e s (this S ho
ppi n gCar t c a r t Pa r am ) { d e c i m a l t o t a l = 0; fo r
e a c h (Product prod in cartP ar am. Pr odu cts ) { t o t a l += p
r o d . P r i c e ; } return t o t a l ; }
}________________________________________________________________________________________
t h i s , , T o t a l P r i c e s . .NET, S h o p p i n g C a r t .
S h o p p i n g C a r t , , c a r t P a r a m . P r o d u c t s S h
o p p i n g C a r t P r o d u c t . P r i c e . 5.11.
! , , . , , .
5.11. u s i n g System; using S y s t e m . C o l l e c t i o n
s . Ge n eri c; c l a s s Program { s t a t i c v o i d M a i n ( s
t r i n g [] a r g s ) { // S ho ppi n g C ar t S h o pp i n g C a
r t c a r t = new S ho ppi ngCar t { P r o d u c t s = new L i s t
< P r o d u c t > { new P r o du c t {Name = " Ka y a k " , P
r i c e = 275M}, new P r o d u c t {Name = " L i f e j a c k e t "
, P r i c e = 48. 95M} , new P r o d u c t {Name = " S o c c e r b
a l l " , P r i c e = 19. 50M} , new P r od u c t {Name = " C o r n
e r f l a g " , P r i c e = 34.95M} } }; // de c i m a l c a r t T
o t a l = c a r t .T o t a l P r i c e s ();
C o n sole.W riteLin e ( " T o t a l : { 0 : c } " , } }
cartTotal);
S h o p p i n g C a r t P r o du c t , . , , . , ,
5.
101
S h o p p i n g C a r t . , , . .NET , .. , u s i n g . , 5.11,
: Total: $378.40
, , , . 5.12 S h o p p i n g C a r t , I E n u m e r a bl e <
P r o d u c t > .
5.12. S h o p p in g C a rtusing S y s t e m . C o l l e c t i o
n s ; using S y s t e m . C o l l e c t i o n s . Gen eri c;*
p u b l i c c l a s s S h o p p i n g C a r t : I E nu me r a bl
e< Pr od u c t > { p u b l i c L i s t < P r o d u c t
> P r o du c t s { g e t ; set; } {
p u b l i c I E n u me r a t o r < P r o d u c t > G et En
um e r at o r () r e tu r n P r o d u c t s . GetEnumerator( ) ; }
I Enume ra t or I E n u m e r a b l e . G e t E n u m e r a t o r
() r e tu r n GetEnumerator( ) ; I } {
, I E n u m e r a b l e < P r o d u c t > (. 5.13).
5.13. , using S y s t e m . C o l l e c t i o n s . Generic; p u
b l i c s t a t i c c l a s s My Ext ensi onMet hods { p u b l i c
s t a t i c d e c i m a l T o t a l P r i c e s ( t h i s
IEnumerable productEnum) d e c i m a l t o t a l = 0; f o r e a c h
( P r o d u c t p r o d i n productEnum) { t o t a l += p r o d . P
r i c e ; } return t o t a l ; } } IE n u m e r a b le < P r o d
u c t> , , f o r e a c h . . , P r o d u c t s , IE n u m e r a
b le < P r o d u c t > , S h o p p in g C a r t, P r o d u c
t s (. 5.14). {
1 02
I. ASP.NET MVC 3
5.14. u si n g System; u si n g S y s t e m . C o l l e c t i o
n s . G e n e r i c ; c l a s s Program { s t a t i c v o id M a in
(s t r in g [ ] args) { // S ho ppi n g C ar t I E n u me r a b l
e< P r o d u c t > p r o d u c t s = new S ho ppi n g C a r t
{ P r o d u c t s = new L i s t < P r o d u c t > { new P r o
d u c t {Name = " Ka y a k , P r i c e = 275M}, new P r o d u c t
{Name = " L i f e j a c k e t " , P r i c e = 48. 95M} , new P r o
du c t {Name = " S o c c e r b a l l " , P r i c e = 19. 50M} , new
P r o du c t {Name = " Co r n e r f l a g " , P r i c e = 34.95M} }
}; // P r o d u c t P r od u c t [ ] p r o d u c t A r r a y = {
new P r o d u c t {Name = " Ka ya k " , P r i c e = 275M}, new P r
o d u c t {Name = " L i f e j a c k e t " , P r i c e = 48. 95M} ,
new P r o d u c t {Name = " S o c c e r b a l l " , P r i c e = 19.
50M} , new P r o d u c t {Name = " C o r n e r f l a g " , P r i c
e = 34.95M} }; // decimal c a r t T o t a l = p r o d u c t s . T o
t a l P r i c e s ( ) ; d e c i m a l a r r a y T o t a l = p r o d
u c t s . T o t a l P r i c e s () ; C onsole.W riteLine("C art T o
t a l: { 0 : c } " , c a r tT o ta l); C on s ole .W rite L in e (
"Array T o t a l : { 0 : c } " , a rr a y T o ta l); } }
! I E n u m e r a b l e < T > C# . MSDN. , , #, . , . , #.
, . , 5.14, , , , P r o d u c t : Cart T o t a l : Array T o t a l
: $378.40 $378.40
, . , I E nu me r a bl e< T > I E nu me r a bl e <
T> , y i e l d , . 5.15.
5.
103
5.15. public static this foreach if IEnum erable F
ilterByCategory( string categoryParam ) { IEnum erable<
Product> (Product prod in == productEnum, { {
productEnum) categoryParam)
(p r o d .C a t e g o r y yield return
prod;
} }
} F ilterB y C atego ry
,
. P r o d u c t , C a t e g o r y , I E n u m e r a b l e < P
r o d u c t > , , . 5.16.
5.16. using using class System ; S y s t e m .C o lle c t io n s
. G en eric; Program void { M a i n ( s t r i n g [] args) {
ShoppingCart new ShoppingCart {
static / /
= {
IEnum erable< Product> Products new = new
products
List {Name = "K ay ak",
Product
Category
=
"W a te rsp o rts", "W atersp o rts", = = "S o c c er", So
ccer",
Price Price Price Price
=
275M }, = 4 8 .9 5 M },
new P r o d u c t new new Product Product
{Name = {Name {Nam e = =
"Lifejacket", "So ccer "C orn er
Category =
b all", fla g ",
Category Category
= =
1 9 .5 0 M }, 3 4 .9 5 M }
}
} ;foreach (Product prod in products. {0},
F i l t e r ByCategory("Soccer" ) )Price { l :c }", p r o d .N a
m e ,
{
C o n s o le .W r i t e L i n e ("N a m e:
p r o d .P r ic e );
}} F i l t e r B y C a t e g o r y S h o p p i n g C a r t P r o
d u c t , S o c c e r . :Name: Name: Soccer Corner ball, flag,
Price Price $ 1 9 .5 0 $ 3 4 .9 5
, , . , P r o d u c t , S o c c e r , T o t a l P r i c e s , P
r i c e , 5.17.
5.17. decim al total = p r o d u c t s . F ilt e r B y C a te g
o r y ("S o c c e r ") . T o t a l P r i c e s (); to tal: {0:c}",
total);
C on sole.W r it e L in e ("F ilt e r e d
104
I. ASP.NET MVC 3
: F i l t e r e d t o t a l : $54.45
- F i l t e r B y C a t e g o r y , . , P r o d u c t , (.
5.18).
5.18. p u b l i c s t a t i c I E n u me r a bl e < P r o d u
c t > F i l t e r ( t h i s I E n u me r a b l e< P r o d u c
t > productEnum,F u n c < P r o d u c t , b ool>
selectorParam) {
f o r e a c h ( P r o d u c t p r o d i n productEnum) i f
(selectorParam (prod)) { y i e l d r e tu r n prod; } } }
{
Fun , . P r o d u c t b o o l , t r u e , P r o d u c t .
5.19.
5.19. Funcu s i n g System; using S y s t e m .C o ll e c t io n
s .Generic; c l a s s Program { s t a t i c v o i d M a i n ( s t r
i n g [] a r g s ) {
// S ho ppi ngCar t I E n u me r a b l e < P r od u c t >
p r o d u c t s = new S ho ppi n gCar t { P r o d u c t s = new L i
s t < P r o d u c t > { new P r o d u c t {Name = " Ka ya k "
, C a t e g o r y = " W a t e r s p o r t s " , P r i c e = 275M},
new Pr oduct {Name = " L i f e j a c k e t " , Ca t eg o r y = " W
a t e r s p o r t s " , P r i c e = 48.95M}, new P r o d u c t
{Name = " S o c c e r b a l l " , C a t e g o r y = " S o c c e r "
, P r i c e = 19. 50M} , new P r o d u ct {Name = " C o r n e r f l
a g " , C a t e g o r y = " S o c c e r " , P r i c e = 34.95M}
}
} ;F u n c < P r o d u c t , b o o l > c a t e g o r y F i
l t e r = d e l e g a t e ( P r o d u c t prod) r e t u r n p r o d
.C a t e g o r y = = "Soccer"; {
} ;I E n u m e r a b l e < P r o d u c t > f i l t e r e d
P r o d u c t s = p r o d u c t s .F i l t e r ( c a t e g o r y F
i l t e r ) ;
fo r e a c h (Product prod in f i l t e r e d P r o d u c t s )
{ Console.WriteLine("Nam e: { 0} , P rice: { l : c } " ,
prod. Name, p r o d . P r i c e ) ;
}}}
5.
105
, Produ ct , , Fun , . -, . , 5 .2 0 .
5.20. - F u n c < P r od u c t , b o o l > c a t e g o r y
F i l t e r = prod => prod.Category == "Soccer"; I E nu me r a b
l e< P r o d u c t > f i l t e r e d P r o d u c t s = p r o
d u c t s . F i l t e r ( c a t e g o r y F i l t e r ) ; - . , .
=> - .
Pr od u ct , prod, b o o l , , C a t e g o r y prod Soccer.
,
Func, 5 .2 1 .
5.21. - FuncI E nu me r a b l e< P r o d u c t > f i l t e
r e d P r o d u c t s = p r o d u c t s . F i l t e r ( pr o d
=> p r o d. C a t e g o r y == " S o c c e r " ) ; -
F i l t e r . , . , - ( . 5 .2 2 ).
5.22. , -I En u me r a b l e < P r od u c t > f i l t e r
e d P r o d u c t s = p r o d u c t s . F i l t e r ( p r o d =>
p r o d . C a t e g o r y == " S o c c e r " | p r o d . P r i c e
> 2 0 ) ; |
- -. , :prod => E v a l u a t e P r o d u c t ( p r o d )
- , , , :(prod, count) => p r o d . P r i c e > 20 &
count > 0 &
, , - , , , ({}) r e t u r n , :(prod, count) => {//. . .
return r e s u l t ;
} - , , . , .
106
I. ASP.NET MVC 3
v a r C# , 5.23. .
5.23. var m y V a r i a b l e = new Pr o d u c t { N a m e =
"Kayak", s tring name = m y V a r i a b l e .Name; int count = m y
V a r i a b l e .C o u n t ;
C a t e g o r y = "Watersports", // //
Price = 275 M };
, m y V a r i a b l e . , . , Product.
, . 5.24.
5.24. var m y A n o n T y p e = new { Name = "MVC", C a t e g o
r y = "Pattern"
} ;C o n s o l e . W r i t e L i n e ( " N a m e : {0}, Type:
{1}", myAnonType.Name, m y A n o n T y p e .C a t e g o r y ) ;
m y A n o n T y p e . , , JavaScript. . - . , , . C# . , , . , ,
5.25.
5.25. var o d d s A n d E n d s new { N ame = new { N ame = new
{ N a m e = = n e w [] { "MVC", C a t e g o r y = "Pattern"},
"Hat", C a t e g o r y = "Clothing"}, "Apple", C a t e g o r y =
"Fruit"}
};foreach (var i t e m in oddsAndEnds) { C o n s o l e .W r i t
e L i n e ("Name : {0}", item.Name);
}
5.
1 07
, v a r . , , , . , , Name . , . , , , .
LINQ LINQ. LINQ . .NET. LINQ, . LINQ S Q L - . , P r o d u c t .
LINQ, , 5.26.
5.26. LINQu s i n g System; using S y s t e m . C o l l e c t i
o n s . G en eri c; c l a s s Program { s t a t i c v o i d M a i n
( s t r i n g [] a r g s ) {
P r o d u c t [] p r o d u c t s = { new P r o du c t {Name = "
K a y a k " , C a t e g o r y = " W a t e r s p o r t s " , P r i c
e = 275M), new Pr oduct {Name = " L i f e j a c k e t " , C a t e g
o r y = " W a t e r s p o r t s " , P r i c e = 48.95M}, new P r o
d u ct {Name = " S o c c e r b a l l " , C a t e g o r y = " S o c
c e r " , P r i c e = 19. 50M} , new P r o du c t {Name = " C o r n
e r f l a g " , C a t e g o r y = " S o c c e r " , P r i c e =
34.95M} }; // P r o d u c t [] r e s u l t s = new P r o d u c t [
3 ] ; // A r r a y . S o r t ( p r o d u c t s , ( i t e m l , i t
em2 ) => { re tu r n Comparer. D e f a u l t . C o m p a r e (i
t e m l . P r i c e , });
item2. P r i c e ) ;
// A r r a y . C o p y ( p r o d u c t s , r e s u l t s , 3) ;
// foreach (Product in r e s u l t s ) { C o n s o l e .W r it e L
in e ( "Item: { 0} , Cost: } } } LIN Q , 5.27.
{1}",
p.Name, p . P r i c e ) ;
108
I. ASP.NET MVC 3
5.27. LINQ u s i n g System; using System .Linq; c l a s s
Program { s t a t i c v o i d M a i n ( s t r i n g [] a r g s ) {
P r o d u c t [] p r o d u c t s = { new P r od u ct {Name = " K a
y a k " , C a t e g o r y = " W a t e r s p o r t s " , P r i c e =
275M}, new Pr oduct {Name = " L i f e j a c k e t " , C a t e g o r
y = " W a t e r s p o r t s " , P r i c e = 48.95M}, new P r od u
ct {Name = " S o c c e r b a l l " , C a t e g o r y = " S o c c e
r " , P r i c e = 19. 50M} , new P r o du c t {Name = " C o r n e r
f l a g " , C a t e g o r y = " S o c c e r " , P r i c e = 34.95M}
};v a r r e s u lt s = fro m p ro d u c t in { p ro d u c ts d e s
c e n d in g o rd e rb y p r o d u c t . P r ic e s e le c t new p
r o d u c t.N a m e , p r o d u c t . P r ic e
>; i n t co unt = 0; // foreach (var p in r e s u lt s ) { Co
n sole.W r i t e L i n e ( "Item : i f ( ++co unt == 3) { break; }
} } }
{0},
Cost:
{1}",
p.Name, p . P r i c e ) ;
. S Q L - . P r o d u c t s e l e c t , , , . LINQ . , P r o d u
c t , . , . , LINQ .
, , . P r o d u c t 5.28.
5.28. LINQu s i n g System; using System .Linq ; c l a s s
Program { s t a t i c v o i d M a i n ( s t r i n g [] a rg s) { Pr
od u c t [] p r o d u c t s = { new P r o du c t {Name = " K a y a
k " , C a t e g o r y = " W a t e r s p o r t s " , P r i c e =
275M}, new Pr oduct {Name = " L i f e j a c k e t " , C a t e g o r
y = " W a t e r s p o r t s " , P r i c e = 48.95M}, new P r o d u
c t {Name = " S o c c e r b a l l " , C a t e g o r y = ' S o c c e
r " , P r i c e = 19. 50M} , new P r o d u c t {Name = " C o r n e
r f l a g " , C a t e g o r y = " S o c c e r " , P r i c e =
34.95M} };
5. v a r r e s u lt s = p ro d u c ts . O r d e r B y D e s c e
n d in g ( = > e . P r i c e ) . T a k e (3 ) . S e l e c t (e =
> new { e .N a m e , e . P r i c e
109
}) ;
foreach (var p in r e s u l t s ) { C o n s o l e . W r i t e L
i n e ( " I t em: } } }
{0},
Cost:
{1}",
p.Name, p . P r i c e ) ;
, , LINQ, , , , LINQ #. LINQ . LINQ, 5.28, I E n u m e r a b l e
< T > I E n u m e r a b l e < T > , .
! LINQ Sys t em. L I N Q , u s i n g , . O r d e r B y D e s c e
n d i n g . - , . ( ). S e l e c t , . , Name P r i c e . , . C# ,
, S e l e c t . LIN Q . 5.1. LIN Q , , , . LINQ, . 5.1, I E n u me
r a b l e < T > .
5.1. LINQ Any Contains Count First FirstOrDefault
t r u e , t ru e , , , t ru e , ,
110
I. ASP.NET MVC 3 . 5.1
Last L a s tO r D e fa u lt
, , - , - , , , , , , ,
M ax M in O rd e rB y O rd e rB y D e s c e n d in g R e v e rs
e S e le c t S e le c tM a n y
S in g le
S in g le O r D e f a u l t
S k ip S k ip W h ile Sum Take T a k e W h ile T o A rra y T o D
ic t io n a r y T o L is t W here
UNO , . 5.1 . LINQ. , , , IE n u m e r a b le < T > ,
5.29.
5.29. LINQ u s in g S y s te m ; u s in g S y s t e m .L in q ;
c la s s P ro g ra m { s t a t i c v o id M a i n ( s t r i n g []
a r g s ) {
5.
111
Product[ products = { ] new Product {Name = "Kayak", Category =
"Watersports", Price = 275M}, new Product (Name = "Lifejacket",
Category = "Watersports", Price = 48.95M}, new Product (Name =
"Soccer ball", Category = "Soccer", Price = 19.50M), new Product
{Name = "Corner flag", Category = "Soccer", Price = 34.95M) }; var
results = products .OrderByDescending(e => e.Price) .Take(3)
.Select(e => new { e.Name, e.Price }); products[2] = new Product
{ Name = "Stadium", Price = 79500M }; foreach (var p in results) {
Console.WriteLine("Item: (0), Cost: {1)", p.Name, p.Price); ) )
_______________________________
Product, , . , Product, . : Item: Stadium, Cost: 79500 Item:
Kayak, Cost: 275 Item: Lifejacket, Cost: 48.95 , , , S t a d i u m
Product . , LINQ. 5.30.
5.30. LINQusing System; using System.Linq; class Program {
static void Main(string [] args) { Product[ products = { ] new
Product {Name = "Kayak", Category = "Watersports", Price = 275M},
new Product {Name = "Lifejacket", Category = "Watersports", Price =
48.95M}, new Product {Name = "Soccer ball", Category = "Soccer",
Price = 19.50M}, new Product {Name = "Corner flag", Category =
"Soccer", Price = 34.95M} }; var results = products.Sum (e =>
e.Price); products[2] = new Product { Name = "Stadium", Price =
79500M }; Console.WriteLine("Sum: } } {0:c}", results);
112
I. ASP.NET MVC 3Sum
Sura: $378.40 , .Stadium ,
:
,
, LINQ, , . 5.31.
5.31. using System; using System.Linq; class Program { static
void Main (string [] args)
{
Product[ products = { ] new Product (Name = "Kayak", Category =
"Watersports", Price = 275M}, new Product (Name = "Lifejacket",
Category = "Watersports", Price = 48.95M}, new Product {Name =
"Soccer ball", Category = "Soccer", Price = 19.50M}, new Product
{Name = "Corner flag", Category = "Soccer", Price = 34.95M} }; var
results = products .OrderByDescending(e => e.Price) .Take (3)
.Select(e => new { e.Name, e,Price }); foreach (var p in
results) { Console.WriteLine("Item: {0}, Cost: {1}", p.Name,
p.Price); } Console.WriteLine("---End of results---"); products[2]
= new Product { Name = "Stadium", Price = 79500M }; foreach (var p
in results) { Console.WriteLine("Item: {0}, Cost:}
{1}", p.Name, p.Price);
}
> , LINQ, . , . : Item: Kayak, Cost: 275 Item: Lifejacket,
Cost: 48.95 Item: Corner flag, Cost: 34.95 - End of results--Item:
Stadium, Cost: 79500 Item: Kayak, Cost: 275 Item: Lifejacket, Cost:
48.95 - End of results---
5.
113
, . , - L1NQ. , , , . , , , .
LINQ IQ u e r y a b le < T > LINQ, . LINQ to Objects, .
LINQ to Objects #, . LINQ to XML , XML-. LINQ LINQ to Objects, .
LINQ to Entities, LINQ , Entity Framework. , Entity Framework ORM-
Microsoft, ADO.NET. 0RM , #, , . Entity Framework LINQ to Entities
, , LINQ, IQ u eryable. lQ u e ry a b le < T > IE n u m
erable , . SQL Server. lQ u erya b le< T > . LINQ , (, XML,
..). IQ u erya b le< T > , , .
RazorRazor MVC 3. ASP.NET -, , . , ASPX , ASP.NET. Razor MVC , @
, . , . Razor, , . Razor, . Razor .
114
I. ASP.NET MVC 3
Razor, M VC. , 3, M V C 3. Razor.
, Product. Product.cs Models 5.32.
5,32. namespace Razor.Models { public class Product { public
public public public public } } Product, . int ProductID { get;
set; } string Name { get; set; } string Description { get; set; }
decimal Price { get; set; } string Category { set; get; }
Controllers Add (), Controller (). ProductController (Controller
name) Empty controller ( ) Template (), . 5.1.
. 5 .1 . P r o d u c t C o n t r o l l e r
5 .
115
A dd (), C o n t r o l l e r , , 5.33.
5.33. using System.Web.Mvc; using Razor.Models; namespace
Razor.Controllers { public class ProductController : Controller {
public ActionResult Index!) { Product myProduct = new Product {
ProductID = 1, Name = "Kayak", Description = "A boat for one
person", Category = "Watersports", Price = 275M
} ;return View(myProduct);
) }
____________________________________________________________________
Razor, M V C . Index P roduct View . )
, Index ProductController Add View ( ). Create a stro n g ly ty
p e d view ( ) P ro d u ct (Razor. M odels), . 5.2.
. 5 .2 . Index
116
I ASP.NET MVC 3 .
! Product , . Visual Studio , .
Use a layout or m aster page ( ). , . Add (), , Views/Product
Index.cshtml.
MVC, URL- / Index Product. Global.asax RegisterRoutes.
routes.MapRoute. , controller, Default Product, 5.34.
5.34, routes.MapRoute( "Default", //
"{controller}/{action}/{id}", // URL new { controller = "Product",
action = "Index", id = UrIParameter.Optional }
); , controller, Default Product . . 11.
Razor Razor .cshtml, .aspx, MVC ASP.NBT Web Forms. MVC 3 - ASPX,
Razor, , , MVC Microsoft. Index.cshtml , , 5.35. Razor.
5.35. Razor@model R a z o r . M o d e l s . P r o d u c t@{
View Bag. T itle
= "In d ex ";
} In dex< /h 2>
, , 5.35, . : Smodel Razor.Models.Product
5.
117
Razor 0. , @model. 3, . , @Model, 5.36. 5.36. Srnodel
Razor.Models.Product0{
ViewBag.Title = "Index";
} Name: 0Model. Name , Razor , , . , m (@model), (SModel). ( S
ta r t Debugging ( ) Debug () Visual Studio), , . 5.3.
. 5.3. 5.36
Razor 5.36 , Razor , :Name: @ M o d e l. Nam e
, . #, 5.37. 5.37, RazorSmodel Raz o r .M o d e l s .Product0{V
ie w B a g .T itle = "Ind ex";
}Name: 0 M o d e l . Nam e 0 D a t e T i m e . N o w . T o S h o
r t T i m e S t r i n g ()
Time v i e w r e n d e r e d :
. Razor HTML- . 0Model , , 5.37 .
118
I ASP.NET MVC 3 .
Razor . #, 5.38.
5.38. 0model R a z o r .M o d e l s .Product @{View Bag.T it le
= "Ind ex";
}Name: 0 M o d e l .N am e < / h 2 >
@if (Model.Category == "Watersports") { 0Model.Category
Splash!
}Time v i e w rendered: S D a t e T i m e .N o w . T o S h o r t
T i m e S t r i n g ( )
if, , C a t e g o r y P r o d u c t W a t e r s p o r t s .
Razor , , i f HTML-, , . -, . 5.4.
. 5.4. if
, HTML-, Razor . 5.39.
5.39. , HTML-Smodel R a z o r .M o d e l s .Product 0{ V i e w B
a g .Title = "Index";
Name: S M o d e l .N a me < / h 2 > 0if (S M o d e l .C a
t e g o r y == "Watersports") { 0:Category: S M o d e l .C a t e g
o r y < b > S p l a s h !
) Time view rendered: S D a t e T i m e .N o w .T o S h o r t T
i m e S t r i n g ( )
5.
119
, H T M L -, . @: Razor, , H T M L - , Razor, . . 5.5.
. 5.5. , H T M L -, t e x t , 5.40. @:.
5.40. T e x tSmodel R a z o r . M o d e l s . P r o d u c t
@{View Bag. T itle = "Ind ex";
}Name: Sif S M o d e l . Nam e< /h2> "W atersports") ( (S
M o d e l.C a te g o r y ==
Category: SModel.Category Splash! Row, row, row your
boat, Gently down the stream...
} Time v i e w rendered: S D a t e T i m e . N o w . T o S h o r
t T i m e S t r i n g ()
t e x t - Razor HTM L. @:, . . 5.6.
. 5 .6 . t e x t
120
I. ASP.NET MVC 3
, @{ }. 5.41. 5 .4 1 .
0model Razor.Models.Product@{
ViewBag.Title = "Index";}
Name: SModel.Name@{
if (Model.Category == "Watersports") { Category: @Model.Category
Splash!
}if (Model.Price > 1 0 ) Pricey! {
})
i f , . . 5.7.
. 5 .7 .
, . ( ), .
ViewBag , ViewData . ViewBag. Product 5.42.
5 .4 2 . V ie w B a g using System; using System.Web.Mvc; using
Razor.Models;namespace R a z o r .Controllers {
5. public class ProductController : Controller { public
ActionResult Ind e x ( { ) Product myProduct = new Product {
ProductID = 1, Name = "Kayak", Description = "A boat for one
person", Category = "Watersports", Price = 275M
121
} ;ViewBag.ProcessingTime = DateTime.N o w .ToShortTimeString();
return View(myProduct); } ) } ViewBag , . P r o c e s s i n g T i m
e , . ViewBag , 5.43.
5.43. V ie w B a g 0model Raz o r .M o d e l s .Product
0{ViewBag.Title = "Index";
}Name: @Model.Name 0{ if (Model.Category == "Watersports") {
Description: SModel.Description (Splash!) < / b x / p >
Category: @Model.Category } else { @ :Description:
SModel.Description } } View rendered at SViewBag.ProcessingTime .
5.8. . 5 .8 . V ie w B a g 122 I ASP.NET MVC 3 . ViewBag ViewData ,
, . , , Visual Studio, :@{ViewBag.Title = "Index"; } , Index Title.
, . , , Visual Studio, . Add View ( ), (. 5.9).. 5 .9 . Razor ,
_viewstart. Views M VC, _ViewStart.cshtml, 5.44. 5.44. _ V i e w S
t a r t . c s h t m l@ {Layout = "~/Views/Shared/_Layout.cshtml"; }
M V C _ViewStart.cshtml , . , _L a y o u t . c s h t m l
Views/Shared. ! , ( _) , , . , Visual Studio, , M V C . , , , . -
5. 123, , ViewBag Index. _Layout.cshtml 5.45. 5.45. _ L a y o u t
.c s h t m l @ViewBag.Title rel="stylesheet" 0RenderBody( ) - ASPX.
ViewBag.Title, Index, H T M L - title. A S P X Razor @ R enderBody
(). -, Razor H T M L -, , . H T M L -, Index HTML- body. Razor .
Use layout or master page , , 5.46. 5.46. , 0{Layout = null;
IndexNoTemplate - , , H T M L -, html, hea d body.124 I. ASP.NET
MVC 3 , Layout n ull. , _V iew S tart. cshtml - . , M VC. LINQ, . ,
LINQ, M VC. Razor, M V C 3. Razor , A S P X - , , , , , . , M V C .
*6, MVC , MVC. : DI, . , . , . , , . 4, DI Ninject. , . , , Ninject
. , , , DI Ninject . Ninject , Unity Microsoft. , Visual Studio
2010. , NUnit, .NET. NUnit, , Visual Studio 2010 , (integrated
development environment IDE) . Moq, . Moq , . Moq, ; . , . , Rhino
Mocks . . , , , .12 6 I. ASP.NET MVC 3 Ninject DI 4. , M VC, DI.
6.1 , , . 6.1. , public class Product { public int ProductID { get;
set; ) public string Name ( get; set; } public string Description {
get; set; } public decimal Price { get; set; } public string
Category { set; get; } } public interface IValueCalculator {
decimal ValueProducts(params Product[] products); ) public class
LinqValueCalculator : IValueCalculator { public decimal
ValueProducts(params Product!] products) return p roducts.Sum (p
=> p.Price);(} } Product , 5. I V a l u e C a l c u l a t o r ,
P r o d u c t . LinqValueCalculator, S um LINQ, Price Product. ,
IValueCalculator, DI. 6.2. 6.2. I V a l u e C a l c u l a t o
rpublic class ShoppingCart ( private IValueCalculator calculator;
public ShoppingCart(IValueCalculator calcParam) ( calculator =
calcParam; ) public decimal CalculateStockValue( ( ) // Product[]
products = ( new Product ( { Name = "Kayak", Price = 275M), ) new
Product!) { Name = "Lifejacket", Price = 48.95M], new Product ( (
Name = "Soccer ball", Price = 19.50M}, ) new Product ( { Name =
"Stadium", Price = 79500M} )};// decimal totalValue =
calculator.ValueProducts(products); // return totalValue; ) 6 ,
.127 . (DI) S h o p p i n g C a r t IValueCalculator . C a l c u l
a t e S t o c k V a l u e Product, ValueProducts IValueCalculator,
, . . 6.1, , Sh o ppingCart LinqValueCalculator.. 6.1. S h o p p i
n g C a r t LinqValueCalculator IValueCalculator, S h o p p i n g C
a r t LinqValueCalculator; , LinqValueCalculator. L i n q V a l u e
C a l c u l a t o r IValueC a l c u l a t o r , S h o p p i n g C a
r t . ! Prod u c t . . Product , , . MVC, Product. ShoppingCart
IValueCalculator . Ninject DI. Ninject , Visual Studio. .