Developing ASP.net MVC4 Applications

download Developing ASP.net MVC4 Applications

of 22

Transcript of Developing ASP.net MVC4 Applications

  • 7/23/2019 Developing ASP.net MVC4 Applications

    1/22

    1.Exploring ASP.NET MVC 4Overview of Microsoft Web Tecnologies

    WebMatrix 2! "o# c$n #se WebM$trix % to cre$te st$tic &TM' p$ges $n( ()n$*ic p$ges witASP.NET+ P&P+ $n( No(e.,s. WebM$trix %is $ free (evelop*ent tool t$t )o# c$n inst$ll b)(ownlo$(ing $n( #sing te Microsoft Web Pl$tfor* -nst$ller Web P-/ fro* te Microsoft website.WebM$trix% en$bles )o# to (evelop c#sto* websites b$se( on pop#l$r web $pplic$tions s#c $sOrc$r(+ 0*br$co CMS+ $n( Wor(Press. 0sing WebM$trix %+ )o# c$n cre$te ASP.NET Web P$ges$pplic$tions+ b#t not ASP.NET Web or*s or MVC $pplic$tions

    Overview of ASP.NET 4.2

    Progr$**ing Mo(els Web Pages:P$ges cont$in server co(e C3 co(e+ p$ges $ve $ .cst*l le extension+ V5

    ! .vbt*l/. Tis progr$**ing *o(el is si*ple $n( e$s) to le$rn+ $n( is s#ite( for si*ple($t$!(riven sites. ASP.NET 4.2 incl#(es Web P$ges version %.6.

    Web Forms:Ver) si*il$r to b#il(ing (es7top $pplic$tions. S#pports #s$ge of pre(ene(

    set of controls t$t c$n be (r$gge( $n( (roppe( on te for*+ ($t$ so#rce c$n $lso be$ssigne( b) (r$g $n( (rop."o# c$n $lso $(( tir(!p$rt) controls or b#il( c#sto* controls. To respon( to #ser $ctions+)o# c$n $tt$c event $n(lers cont$ining co(e to te server!si(e controls.

    MVC8 server!si(e co(e is split into 9 p$rts8

    o ModelCl$sses t$t represent te ($t$ or te b#siness logic. Mo(el cl$sses $re $lsoresponsible for writing $n( re$(ing te ($t$ to te ($t$b$se.

    o Controller&$n(les #ser inter$ction $n( re:#ests+ cre$tes $n( *o(ies *o(el cl$sses+ $n(selects $ppropri$te views.

    o View

  • 7/23/2019 Developing ASP.net MVC4 Applications

    2/22

    0se( to vis#$li;e te 0- controls $n( to (ispl$) te* to te #sers. Controllersoften p$ss $n inst$nce of $ *o(el cl$ss to $ view. Te view (ispl$)s properties ofte *o(el cl$ss.

    Te ASP.NET AP-System.Web n$*esp$ce8

    Confguration0sing web.cong les+ )o# c$n cong#re )o#r web $pplic$tion+ reg$r(less of te progr$**ing*o(el. -n co(e+ )o# c$n $ccess te cong#r$tion tro#g teSystem.Web.Confgurationn$*esp$ce.

    Authentication and AuthorizationM$n) websites re:#ire #sers to log on b) entering $ #ser n$*e $n( p$sswor(+ or b) provi(ingextr$ infor*$tion. "o# c$n #se ASP.NET *e*bersip provi(ers to $#tentic$te $n( $#tori;e#sers $n( restrict $ccess to content System.Web.Security N$*esp$ce/

    CachingASP.NET c$ces $ ren(ere( p$ge in *e*or)+ so t$t it c$n ret#rn te s$*e p$ge to s#bse:#ent#ser re:#ests wito#t $ving to ren(er it $g$in fro* te st$rt. -n $ si*il$r *$nner+ .NETr$*ewor7 ob,ects c$n $lso be c$ce(.

    "o# c$n $ccess c$ce( p$ges b) #sing te System.Runtime.Caching n$*esp$ce $n( cong#rete c$ces in web.cong.

    Co*piling ASP.NET Co(e

    1. Compilation to MSIL. Wen )o# b#il( $ website in Vis#$l St#(io+ te ASP.NET co*pilercre$tes .(ll les wit $ll te co(e co*pile( into Microsoft -nter*e(i$te '$ng#$ge MS-'/. Tisco(e is bot in(epen(ent of te l$ng#$ge )o# #se( to write te $pplic$tion $n( te CP0$rcitect#re of te server.

    %. Compilation to native code. Wen $ p$ge is re:#este( for te rst ti*e+ te Co**on'$ng#$ge #er)

    A=A?A=A? is $ tecnolog) t$t en$bles browsers to co**#nic$te wit web servers $s)ncrono#sl)

    #sing te ?*l&ttp

  • 7/23/2019 Developing ASP.net MVC4 Applications

    3/22

    Centralized Web Farm ManagementWen )o# r#n $ l$rge website+ )o# c$n cong#re $ lo$(b$l$nce( f$r* of *$n) --S servers tosc$le to l$rge si;es. --S *$n$ge*ent tools *$7e it e$s) to (eplo) sites to $ll servers in te f$r*$n( *$n$ge sites $fter (eplo)*ent.

    Hig Per!ormance Caces

    "o# c$n cong#re ASP.NET to *$7e opti*$l #se of te --S c$ces to $cceler$te responses to #serre:#ests. Wen --S serves $ p$ge or oter content+ it c$n c$ce it in *e*or) so t$t s#bse:#enti(entic$l re:#ests c$n be serve( f$ster.

    "#tentication and Sec#rity--S s#pports *ost co**on st$n($r(s for $#tentic$tion+ incl#(ing S*$rt C$r( $#tentic$tion $n(-ntegr$te( Win(ows $#tentic$tion. "o# c$n $lso #se SS' to encr)pt sec#rit)!sensitiveco**#nic$tions+ s#c $s login p$ges $n( p$ges cont$ining cre(it c$r( n#*bers.

    "SP$%&' S#pport--S is te onl) web server t$t f#ll) s#pports ASP.NET.

    (ter Server)Side 'ecnologies"o# c$n ost websites (evelope( in P&P $n( No(e.,s on --S.

    Sc$ling 0p --SA single web server $s li*ite( sc$l$bilit) bec$#se it is li*ite( b) its processor spee(+ *e*or)+(is7 spee( B#rter*ore+ single web servers $re v#lner$ble to $r(w$re f$il#res $n( o#t$ges.

    "o# c$n i*prove te sc$l$bilit) $n( resilience of )o#r website b) osting it on $ *#ltiple serverf$r*. -n s#c server f$r*s+ *$n) servers s$re te s$*e server n$*e.

    All servers c$n respon( to browser re:#ests. A lo$( b$l$ncing s)ste* s#c $s Win(ows Networ7'o$( 5$l$ncing or $ $r(w$re!b$se( s)ste* s#c $s

  • 7/23/2019 Developing ASP.net MVC4 Applications

    4/22

    encr)ption in Vis#$l St#(io evelop*ent Server.

    Win(ows A;#re &osting"o# c$n #se Win(ows A;#re to ost te following8Websites! Win(ows A;#re s#pports websites (evelope( in ASP.NET+ P&P+ or No(e.,s. "o# c$n$lso (eplo) websites to Win(ows A;#re (irectl) fro* Vis#$l St#(io %61% or WebM$trix %.

    Web services )"o# c$n c$ll web services fro* *obile (evices+ (es7top $pplic$tions+ $n( fro*website co(e r#nning on te client!si(e or te serversi(e.

    Databases! Wen )o# ost $ website in Win(ows A;#re+ )o# c$n #se S>' $t$b$se+ wic is $clo#(!b$se( ($t$b$se service b$se( on S>' Server+ to ost )o#r ($t$b$se.

    Virtual servers )"o# c$n provision entire virt#$l servers in Win(ows A;#re to r#n b#siness!critic$l b$c7 oce softw$re or #se te virt#$l servers $s test environ*ents. Virt#$l servers inWin(ows A;#re c$n r#n Win(ows Server or 'in#x.

    Mobile Services! -f )o# (evelop $pps for *obile (evices s#c $s pones $n( t$blets+ )o# c$n#se Win(ows A;#re to ost services t$t #n(erpin te*. S#c services c$n provi(e #serpreference stor$ge+ ($t$ stor$ge $n( oter f#nctions.

    Media Services)Wen )o# #se Win(ows A;#re to ost *e(i$ s#c $s $#(io $n( vi(eo+ it is$#to*$tic$ll) $v$il$ble to *$n) t)pes of (evices s#c $s co*p#ters+ *obile pones+ $n( t$blets$n( enco(e( in v$rio#s for*$ts s#c $s MP4 $n( Win(ows Me(i$ for*$ts.

    Overview of ASP.NET 4.2

    Web P$ges Applic$tionsWeb P$ges is te si*plest progr$**ing *o(el )o# c$n coose to b#il( $ web $pplic$tion inASP.NET

    Tere is $ single le $ssoci$te( wit e$c webp$ge t$t #sers c$n re:#est. or e$c p$ge+ )o#write &TM'+ client!si(e co(e+ $n( server!si(e co(e in te s$*e .cst*l le C3+ for V5 it is.vbt*l le/.

    Te D s)*bol is #se( to (isting#is server!si(e co(e fro* &TM' $n( =$v$Script.

    Wen #sers re:#est te p$ge+ te ASP.NET r#nti*e co*piles $n( r#ns te server!si(e co(e toren(er &TM' $n( ret#rns t$t &TM' to te web browser for (ispl$).

    Tis is te onl) *o(el t$t it is s#pporte( b) WebM$trix %

    %FSpeci$l OGersH%F

    pFIet te best possible v$l#e on Nortwin( speci$lt) foo(s b) t$7ing $(v$nt$ge of tese oGers8HpF

    Dfore$c v$r ite* in oGers/ J (iv cl$ssKLoGer!c$r(LF (iv cl$ssKLoGer!pict#reLF

    Dif String.-sN#llOrE*pt)ite*.Poto0rl//J i*g srcKLD&refite*.Poto0rl/ $ltKLDite*.TitleL HF H(ivF H(ivF

    Te Web P$ges progr$**ing *o(el $s te following $(v$nt$ges8

    -t is si*ple to le$rn

    -t provi(es precise control over te ren(ere( &TM'

  • 7/23/2019 Developing ASP.net MVC4 Applications

    5/22

    -t c$n be written in WebM$trix % or Vis#$l St#(io%61%

    0sing $ Web P$ges site $s so*e (is$(v$nt$ges8

    -t provi(es no control over 0

  • 7/23/2019 Developing ASP.net MVC4 Applications

    6/22

    Controls c$n $(( l$rge $*o#nts of *$r7#p $n( st$te infor*$tion to te ren(ere( &TM'

    p$ge. Tis incre$ses te ti*e it t$7es to lo$( p$ges.

    W) so#l( )o# be concerne( $bo#t te *$r7#p $n( st$te infor*$tion t$t ASP.NET Web or*scontrols $(( to $ ren(ere( &TM' p$ge

    MVC Applic$tionsASP.NET 4.2 incl#(es MVC version 4.6.

    Models

    Mo(els $re represent$tion of ($t$ $n( te) $re i*ple*ente( $s c#sto* .NET cl$sses $n( store

    co(e in .cs les.

    Vie#s

    Views cre$te te #ser interf$ce.

    Views $re *$r7#p p$ges t$t store bot &TM' $n( C3 co(e in .cst*l les. Tis *e$ns t$t te)

    $re li7e Web P$ges+ b#t te) incl#(e onl) #ser interf$ce co(e. Oter logic is sep$r$te( intoMo(els $n( Controllers.

    Controllers

    Controllers respon( to #ser $ctions+ lo$( ($t$ fro* $ *o(el+ $n( p$ss it to $ view+ so t$t it willren(er $ webp$ge. Controllers i*ple*ent inp#t logic $n( tie Mo(els to te rigt Views.

    Controllers $re .NET cl$sses t$t inerit fro* te System.Web.M!c.Controller cl$ss $n( storeco(e in .cs les.

    d!antages and Disad!antages o" MVCTe MVC progr$**ing *o(el $s te following $(v$nt$ges8

    Views en$ble te (eveloper to t$7e precise control of te &TM' t$t is ren(ere(. "o# c$n #se te

  • 7/23/2019 Developing ASP.net MVC4 Applications

    7/22

    "o# c$n cong#re ASP.NET sites b) cre$ting $n( e(iting web.cong les. Te web.cong le in teroot fol(er of )o#r site cong#res te entire site+ b#t )o# c$n overri(e tis cong#r$tion $t lowerlevels b) cre$ting web.cong les in s#b!fol(ers.

    -f )o# nee( to $ccess cong#r$tion v$l#es $t r#nti*e in )o#r server!si(e .NET co(e+ )o# c$n #sete System.Web.Confguration n$*esp$ce.

    uthentication

    ASP.NET s#pports sever$l *ec$nis*s for $#tentic$tion. -f )o# $re #sing -nternet Explorer on $Win(ows co*p#ter+ ASP.NET *$) be $ble to #se -ntegr$te( Win(ows $#tentic$tion. -n tis*ec$nis*+ )o#r Win(ows #ser $cco#nt is #se( to i(entif) )o#.

    or -nternet sites+ )o# c$nnot be s#re t$t #sers $ve Win(ows+ $ co*p$tible browser+ or $n$ccessible $cco#nt+ so or*s A#tentic$tion is often #se(. or*s A#tentic$tion is s#pporte( b)*$n) browsers $n( it c$n be cong#re( to cec7 cre(enti$ls $g$inst $ ($t$b$se+ (irector)service+ or oter #ser $cco#nt stores.

    Membershi$ and Roles-n ASP.NET+ $ *e*bersip provi(er is $ co*ponent t$t i*ple*ents #ser $cco#nt *$n$ge*ent

    fe$t#res.Sever$l *e*bersip provi(ers $re s#pporte( b) ASP.NET+ s#c $s te S>' Me*bersip Provi(er+wic #ses $ S>' ($t$b$se to store #ser $cco#nts. "o# c$n $lso cre$te $ c#sto* *e*bersipprovi(er+ ineriting fro* one of te (ef$#lt provi(ers+ if )o# $ve #ni:#e re:#ire*ents.

    Wen )o# $ve *ore t$n $ few #sers+ )o# *$) w$nt to gro#p te* into roles wit (iGerentlevels of $ccess. or ex$*ple+ )o# *igt cre$te $ QIol( Me*bersR role cont$ining #ser $cco#ntswit $ccess to te best speci$l oGers. ASP.NET role provi(ers en$ble )o# to cre$te $n( pop#l$teroles wit te *ini*#* of c#sto* co(e.

    "o# c$n en$ble $ccess to p$ges on )o#r website for in(ivi(#$l #ser $cco#nts or for $ll *e*bersof $ role.Tis process is 7nown $s $#tori;$tion.

    State ManagementWeb servers $n( web browsers co**#nic$te tro#g &TTP. Tis is $ st$teless protocol ! $n)v$l#es fro* previo#s re:#ests $re not $#to*$tic$ll) re*e*bere(.

    &owever+ wen )o# b#il( $ web $pplic$tion+ )o# *#st fre:#entl) preserve v$l#es $cross *#ltiplep$ge re:#ests.

    ASP.NET provi(es sever$l loc$tions were )o# c$n store v$l#es or st$te infor*$tion $cross*#ltiple re:#ests.

    CachingAn ASP.NET p$ge is b#ilt ()n$*ic$ll) b) te ASP.NET r#nti*e on te web server. or ex$*ple+ in$ Web P$ges $pplic$tion+ te r#nti*e *#st exec#te te C3 co(e in te p$ge to ren(er &TM' to

    ret#rn it to te browser. T$t C3 co(e *$) perfor* co*plex $n( ti*e!cons#*ing oper$tions.

    "o# c$n #se te ASP.NET p$ge c$ce to store te ren(ere( version of $ co**onl) re:#este( p$gein te *e*or) of te web server.

    C$n )o# tin7 of oter f$cilities t$t $ll ASP.NET $pplic$tions *igt nee(+ reg$r(less of teprogr$**ing*o(el te) #se

  • 7/23/2019 Developing ASP.net MVC4 Applications

    8/22

    -ntro(#ction to ASP.NET MVC 4

    Mo(els+ Views+ $n( Controllers

    Mo(els $n( $t$A *o(el is $ set of .NET cl$ss t$t represents ob,ects $n(le( b) )o#r website.

    "o# c$n $lso incl#(e oter proce(#res+ if necess$r). Tese proce(#res enc$ps#l$te te b#sinesslogic. or ex$*ple+ )o# co#l( write $ Publish proce(#re t$t *$r7s te Pro(#ct $s re$()!to!sell.

    -n $n MVC $pplic$tion+ te *o(el incl#(es co(e t$t re$(s $n( writes ($t$b$se recor(s.

    ASP.NET MVC wor7s wit *$n) (iGerent ($t$ $ccess fr$*ewor7s. Te *ost co**onl) #se(fr$*ewor7 is te Entit) r$*ewor7 version 2.6.

    Controllers $n( ActionsController respon(s to web browser re:#ests in $n MVC $pplic$tion. Tere is #s#$ll) onecontroller cl$ss for e$c *o(el cl$ss. Controllers incl#(e $ctions+ wic $re *eto(s t$t r#n inresponse to $ #ser re:#est.

    Controllers inerit fro* te System.Web.M!c.Controller b$se cl$ss. Actions #s#$ll) ret#rn $System.Web.M!c.ctionResult ob,ect.

    Views $n(

  • 7/23/2019 Developing ASP.net MVC4 Applications

    9/22

    D&t*l.ispl$)N$*eor*o(el KF *o(el.5o()/H(ivF(iv cl$ssKL(ispl$)!el(LFD&t*l.ispl$)or*o(el KF *o(el.5o()/H(ivFHel(setF

  • 7/23/2019 Developing ASP.net MVC4 Applications

    10/22

    Best Practice: 0se Web or*s wen )o# w$nt to cre$te $ #ser interf$ce b) (r$gging controlsfro* $ tool box onto e$c webp$ge or wen )o#r (evelopers $ve experience of Web or*s orWin(ows or*s.

    Best Practice: 0se MVC wen )o# w$nt te *ost precise control of &TM' $n( 0

  • 7/23/2019 Developing ASP.net MVC4 Applications

    11/22

    Client-Side State Storage-f )o# store st$te infor*$tion on client si(e+ )o# will s$ve on server reso#rces. &owever+ $ll client!si(e st$te infor*$tion *#st be sent between te web server $n( te web browser+ $n( tisprocess c$n slow (own p$ge lo$( ti*e. 0se client!si(e st$te stor$ge onl) for s*$ll $*o#nts of($t$.

    Possible #s$ges8Coo0ies* Coo7ies $re s*$ll text les t$t )o# c$n p$ss to te browser to store infor*$tion. Acoo7ie c$n be store(8

    -n te client co*p#ter *e*or)+ in wic c$se it preserves infor*$tion onl) for $ single

    #ser session

    On te client co*p#ter $r( (is7 (rive+ in wic c$se it preserves infor*$tion $cross

    *#ltiple sessions.

    Most browsers c$n store coo7ies onl) #p to 46 b)tes $n( per*it onl) %6 coo7ies per website.Also so*e #sers *$) (is$ble coo7ies for priv$c) p#rposes+ so )o# so#l( not rel) on coo7ies forcritic$l f#nctions.

    /uery strings* A :#er) string is te p$rt of te 0

  • 7/23/2019 Developing ASP.net MVC4 Applications

    12/22

    5) (ef$#lt+ session st$te #ses coo7ies to i(entif) #sers b#t )o# c$n cong#re ASP.NET to storesession st$te wito#t #sing coo7ies.

    Profle $ro$ertie-f )o#r site #ses $n ASP.NET prole provi(er+ )o# c$n store #ser preferences in proles. Proleproperties $re persiste( to te *e*bersip ($t$b$se so te) will be 7ept even if te web

    $pplic$tion or web server rest$rts.

    Database tables-f )o#r site #ses $n #n(erl)ing ($t$b$se+ li7e *ost sites (o+ )o# c$n store st$te infor*$tion in itst$bles. Tis is $ goo( pl$ce to store l$rge vol#*es of st$te ($t$ t$t c$nnot be pl$ce( in server*e*or) or on te client co*p#ter.

    5e$r in *in( t$t st$te ($t$ is onl) one for* of infor*$tion t$t $n ASP.NET $pplic$tion pl$ces inserver *e*or). or ex$*ple+ c$ces *#st s$re *e*or) wit st$te ($t$.

    /uestion:"o# sow te visitors of )o#r website $ coice of co#ntries. Wen te) pic7 $ st$te+)o# w$nt to re(irect te* to $ p$ge t$t sows $ *$p of t$t st$te. "o# will not #se te n$*e ofte cosen co#ntr) t$t te #ser selecte( $fter tis. Wic loc$tion so#l( )o# #se to store ten$*e of te cosen co#ntr)

    Pl$nning Ilob$li;$tion $n( 'oc$li;$tion

    Managing Browsers or Languages and egionsCo(es $re split in two p$rts8

    1. Te l$ng#$ge

    %. Te region

    3sing Resource Files

    -n(ex.es!C'.resx + c$n specif) (ef$#lt l$ng#$ge

    3sing Se$arate Vie#s

    Wen )o# #se sep$r$te views to glob$li;e $n( loc$li;e $ site+ views $re *ore re$($ble+ bec$#se*ost of te text $n( l$bels re*$in in te view le. &owever+ )o# *#st cre$te view les+ wicre:#ires )o# or )o#r te$* *e*bers to be procient in te t$rget l$ng#$ge.

    esigning Mo(els+ Controllers+ $n( Views

    esigning Mo(els)denti"ying Model Classes and Pro$ertiesDomain Model and ,ogical Data Model Diagrams

    "o# c$n #se o*$in Mo(el i$gr$*s $n( 'ogic$l $t$ Mo(el 'M/ (i$gr$*s to $n$l);e teinfor*$tion )o#r website *$n$ges $n( s#ggest $ p)sic$l ($t$ *o(el or ($t$b$se (esign. E$cob,ect in )o#r o*$in Mo(el or 'M (i$gr$* so#l( be i*ple*ente( $s $n MVC *o(el cl$ss.

    Relationshi$s and ggregatesWen )o# i(entif) te *o(el cl$sses t$t )o# will i*ple*ent in )o#r website+ )o# *#st $lsoconsi(er te rel$tionsips between te*. one to *$n)+ one to one+ c$sc$(ing (elteB/

    'ntity Frame#or0

  • 7/23/2019 Developing ASP.net MVC4 Applications

    13/22

    Te Entit) r$*ewor7 is $n Ob,ect ' Server M$n$ge*ent St#(io. Wen )o# #se tis *o(e+ )o#$ve to specif) te ($t$b$se connection string. Te Entit) r$*ewor7 connects to te($t$b$se $n( ex$*ines te ($t$b$se sce*$. -t cre$tes $ set of cl$sses for )o# to #se for($t$ $ccess.

    Model*First8 0se te Entit) r$*ewor7 in te *o(el!rst *o(e wen )o# (o not $ve $preexisting ($t$b$se $n( prefer to (esign )o#r *o(el in Vis#$l St#(io. "o# c$n #se teEntit) esigner tool to n$*e+ cong#re+ $n( lin7 )o#r *o(el cl$sses. Tis cre$tes ?M'les wic te Entit) r$*ewor7 #ses bot to cre$te *o(el cl$sses $n( to cre$te te($t$b$se wit its t$bles $n( rel$tionsips.

    Code*First8 0se te Entit) r$*ewor7 in te co(e!rst *o(e wen )o# $ve no pre!existing ($t$b$se $n( prefer to (esign )o#r *o(els entirel) in C3 co(e. "o#r co(e *#stincl#(e DBConte4t $n( DBSet ob,ects tese correspon( to te ($t$b$se $n( its t$bles.Wen )o# r#n te $pplic$tion for te rst ti*e+ te Entit) r$*ewor7 cre$tes te($t$b$se for )o#. TOO8 oes it cre$te it for te oter *o(els if we (rop te ($t$b$se

    Te Entit) r$*ewor7 lets )o# wor7 in te Agile or Extre*e Progr$**ing st)les. or ex$*ple+ tefr$*ewor7 c$n #p($te te ($t$b$se wen te *o(el c$nges. Altern$tivel) te fr$*ewor7 c$n#p($te te *o(el wen te ($t$b$se c$nges. Te Entit) r$*ewor7 c$n perfor* tese #p($tes

    in $n) Entit) r$*ewor7 *o(e.

    esigning Controllers

    !denti" Controllers and Actions-n $n ASP.NET MVC web $pplic$tion+ tere is #s#$ll) one controller for e$c *o(el cl$ss.Convention$ll)+ if te *o(el cl$ss is c$lle( QPotoR+ te controller is c$lle( QPotoControllerR.

    -f )o# follow tis convention in )o#r (esign+ )o# c$n #se te MVC (ef$#lt ro#ting be$vior toselect te rigt controller for $ re:#est.

    or e$c controller tere c$n be *$n) $ctions e$c $ction is i*ple*ente( $s $ *eto( in tecontroller $n( #s#$ll) ret#rns $ view. "o# often re:#ire sep$r$te $ctions for IET $n( POST &TTP

    re:#est verbs. Si*il$r to (esigning $ *o(el+ )o# c$n i(entif) te $ctions to write in e$ccontroller b) ex$*ining te #se c$ses )o# g$tere( (#ring $n$l)sis.

  • 7/23/2019 Developing ASP.net MVC4 Applications

    14/22

    esigning Views

    Views# $e%&late Views# and 'artial ViewsTere is $ *$n)!to!one rel$tionsip between MVC controllers $n( views.

    or ex$*ple+ $ controller *$) #se one view to (ispl$) $ single poto+ $noter view to (ispl$)sever$l potos+ $n( $ tir( view to en$ble #sers to #plo$( new potos. E$c view correspon(s to$ web p$ge t$t te $pplic$tion c$n (ispl$) to te #ser+ $lto#g it c$n (ispl$) (iGerent ($t$. orex$*ple+ te Potoet$ils view c$n (ispl$) (iGerent potos+ (epen(ing on te - p$r$*eter t$tis p$sse( to it.

    As )o# pl$n views+ )o# so#l( $lso consi(er p$rts of te #ser interf$ce t$t $ppe$r on $ll p$ges.or ex$*ple+ te co*p$n) logo+ *$in site *en#+ lin7s to leg$l infor*$tion+ $n( log on controls*$) nee( to $ppe$r on ever) p$ge. "o# c$n pl$ce tese #ser interf$ce co*ponents in $ te*pl$teview to cre$te $ consistent loo7 $n( feel $cross p$ges.

    &ote:Te*pl$te views in ASP.NET MVC web $pplic$tions perfor* te s$*e role $s *$ster p$gesinASP.NET Web or*s $pplic$tions.

    So*e #ser interf$ce co*ponents (o not $ppe$r on $ll p$ges b#t $re re#se( on sever$l p$ges. orex$*ple+ co**ents *$) be (ispl$)e( in $ single poto (ispl$)+ on te g$ller)+ or on oterp$ges. 5) cre$ting $ p$rti$l view+ )o# c$n cre$te $ re#s$ble #ser interf$ce ele*ent t$t c$n$ppe$r in *$n) loc$tions in tis *$nner+ wito#t (#plic$ting co(e.

    Mo(#le

  • 7/23/2019 Developing ASP.net MVC4 Applications

    15/22

    9. eveloping ASP.NET MVC 4 Mo(elsCre$ting MVC Mo(els

    Te *o(el cl$ss will prob$bl) be revise( wen )o# cre$te te controller $n( views for te *o(el+$n( ten l$ter (#ring te iter$tions t$t $ppen (#ring te pro,ect. -f )o# follow te Agile

    (evelop*ent *o(el or Extre*e Progr$**ing+ )o# begin wit $ si*ple #n(erst$n(ing of te cl$ssper$ps its n$*e $n( $ few properties.

    Wen (eveloping te *o(el+ )o# c$n refer to #se c$ses or #ser stories to ens#re t$t tese*o(el (et$ils $re correct.

    HH ex$*ple of *o(el cl$ssp#blic cl$ss PotoJ p#blic int Poto- J getX setX p#blic string Title J getX setX p#blic b)teYZ Potoile J getX setX p#blic string escription J getX setX p#blic $teTi*e Cre$te($te J getX setX p#blic string Owner J getX setX p#blic virt#$l -CollectionCo**entF Co**ents J getX setX

    Notice t$t te *o(el cl$ss (oes not inerit fro* $n) oter cl$ss. Also+ notice t$t )o# $vecre$te( p#blic properties for e$c propert) in te *o(el $n( incl#(e( te ($t$ t)pe+ s#c $sinteger or string in te (ecl$r$tion. "o# c$n cre$te re$(!onl) properties b) o*itting teset57e)wor(.

    Te Photo cl$ss incl#(es $ propert) c$lle( Comments. Tis is (ecl$re( $s $ collection ofComment ob,ects $n( i*ple*ents one si(e of te rel$tionsip between potos $n( co**ents.Te following lines of co(e ill#str$te ow )o# c$n i*ple*ent teComment *o(el cl$ss.

    p#blic cl$ss Co**entJ p#blic int Co**ent- J getX setX

    p#blic int Poto- J getX setX p#blic string 0serN$*e J getX setX p#blic string S#b,ect J getX setX p#blic string 5o() J getX setX p#blic virt#$l Poto Poto J getX setX

    Notice t$t te Comment cl$ss incl#(es $ Photo)D propert). Tis propert) stores te - of tePhoto t$t te #ser co**ente( on $n( ties te co**ent to $ single poto. Also+ notice t$t teComment cl$ss incl#(es $ Photo propert)+ wic ret#rns te Poto ob,ect t$t te co**entrel$tes to. Tese properties i*ple*ent te oter si(e of te rel$tionsip between potos $n(co**ents.

    HH tis is ow te Mo(el is inst$nti$te( fro* te controllerPoto newPoto K new Poto/X

    newPoto.Title K LTis is $n Ex$*ple PotoLXnewPoto.Owner K 0ser.-(entit).N$*eXnewPoto.Cre$te($te K $teTi*e.To($)Xret#rn ViewLispl$)ViewL+ newPoto/X

    0sing ispl$) $n( E(it $t$ Annot$tions on PropertiesTe *o(el cl$sses #s#$ll) specif) tree $ttrib#tes for e$c propert)8

    Te n$*e of te propert)+ for ex$*ple+ Title.

    Te ($t$ t)pe of te propert)+ for ex$*ple+ String.

  • 7/23/2019 Developing ASP.net MVC4 Applications

    16/22

    Te $ccess levels of te propert)+ for ex$*ple+ te get $n( set 7e)wor(s to in(ic$te re$($n( write $ccess.

    A((ition$ll) b) #sing $ttrib#tes+ )o# c$n s#ppl) $((ition$l *et$($t$ to (escribe properties toASP.NET MVC. Te MVC r#nti*e #ses tis *et$($t$ to (eter*ine ow to ren(er e$c propert) inviews for (ispl$) $n( e(iting. Tese $ttrib#tes $re c$lle( (ispl$) $n( e(it $nnot$tions.

    E.g. Dis$lay&ame $nnot$tion propert) n$*es in C3 c$nnot cont$in sp$ces+ #se ispl$)N$*eto provi(e n$*e cont$ining sp$ces.

    -f )o# $ve $ Date(ime propert)+ )o# c$n #se (ispl$) $n( e(it ($t$ $nnot$tions to infor* MVCw$t for*$t )o# w$nt te propert) (ispl$)e( in.

    Yispl$)N$*eLCre$te( $teL/ZY$t$T)pe$t$T)pe.$teTi*e/ZYispl$)or*$t$t$or*$tString K LJ68((HMMH))L+ Appl)or*$t-nE(itMo(e K tr#e/Zp#blic $teTi*e Cre$te($te J getX setX

    All te ($t$ $nnot$tions t$t $re provi(e( wit ASP.NET MVC 4 $re incl#(e( in teSystem.Com$onentModel.Datannotations n$*esp$ce.

    dditional Reading:To re$( *ore $bo#t $ll te ($t$ $nnot$tions provi(e( wit MVC4+ see ttp8HH*s(n.*icrosoft.co*Hen!#sHlibr$r)Hs)ste*.co*ponent*o(el.($t$$nnot$tions.$spx

    /uestion: -n te co(e on te sli(e+ ow c$n )o# recogni;e te (ispl$) $n( e(it $nnot$tions $n((isting#is te* fro* oter co(e

    V$li($ting 0ser -np#t wit $t$ Annot$tions"o# c$n #se ($t$ $nnot$tions in MVC *o(els to set v$li($tion criteri$ for #ser inp#t. -np#tv$li($tion is te process b) wic MVC cec7s ($t$ provi(e( b) $ #ser or $ web re:#est toens#re t$t it is in te rigt for*$t.

    Te following ex$*ple sows $ webp$ge for* t$t collects so*e infor*$tion fro* te #ser8

    %ame. Tis is re:#ire( inp#t. Te #ser *#st enter so*e ($t$ in tis el(.

    Heigt. Tis *#st be $n integer between 6 $n( 466.

    &mail "ddress. Tis is re:#ire( inp#t. Te v$l#e entere( *#st be $ v$li( e*$il $((ress.

    Te Re1uired+ Range+ String,ength+ $n( Regular'4$ression $nnot$tions i*ple*ent inp#tv$li($tion inMVC.

    To specif) te error *ess$ge t$t te #ser sees wen ($t$ is not v$li(+ )o# c$n #se te'rrorMessage propert) on te v$li($tion ($t$ $nnot$tions

    p#blic cl$ss PersonJp#blic int Person- J getX setX Y

  • 7/23/2019 Developing ASP.net MVC4 Applications

    17/22

    ttp8HHwww.$sp.netH*vcHt#tori$lsHol(er!versionsH*o(els!($t$/Hv$li($tion!wit!te!($t$$nnot$tion!v$li($tors!cs

    dditional Reading: or *ore infor*$tion $bo#t te reg#l$r expressions t$t )o# c$n #se tocec7 #ser inp#t+ see8 ttp8HH*s(n.*icrosoft.co*Hen!#sHlibr$r)Hs6691%.$spx

    /uestion8 "o# w$nt to *$7e s#re t$t #sers enter $ p$sswor( t$t is longer t$n c$r$cters.

    &ow co#l( )o# (o tis b) #sing $ v$li($tion ($t$ $nnot$tion

    W$t Are Mo(el 5in(ersA *o(el bin(er is $ co*ponent of $n ASP.NET MVC $pplic$tion t$t cre$tes $n inst$nce of $*o(el cl$ss+ b$se( on te ($t$ sent in te re:#est fro* te web browser.

    A *o(el bin(er ens#res t$t te rigt ($t$ is sent to te p$r$*eters in $ controller $ction*eto(. Tis en$bles MVC to cre$te inst$nces of *o(el cl$sses t$t s$tisf) te #ser]s re:#est.Te (ef$#lt *o(el bin(er+ for ex$*ple+ ex$*ines bot te (enition of te controller $ctionp$r$*eters $n( te re:#est p$r$*eters to (eter*ine wit re:#est v$l#es to p$ss to wic $ctionp$r$*eter.

    Tis *o(el bin(ing process c$n s$ve (evelopers $ lot of ti*e $n( $voi( *$n) #nexpecte(r#nti*e errors t$t $rise fro* incorrect p$r$*eters. MVC incl#(es $ (ef$#lt *o(el bin(er witsopistic$te( logic t$t p$sses p$r$*eters correctl) in $l*ost $ll c$ses wito#t co*plex c#sto*co(e.

    (he Controller ction )n!o0er and (he De"ault Model BinderTo #n(erst$n( te (ef$#lt *o(el bin(ing process+ consi(er te following re:#est fro* $ webbrowser8ttp8HHwww.$(vent#rewor7s.co*Hpro(#ctH(ispl$)H42Tis re:#est i(enties tree $spects8

    Te *o(el cl$ss ! $ pro(#ct.

    Te oper$tion to perfor* on te *o(el cl$ss so#l( be (ispl$)e(.

    Te specic inst$nce of te *o(el cl$ss ! - 42 is

    Te re:#est is receive( b) $n ob,ect c$lle( te controller $ction invo7er. Te controller $ctioninvo7er of te MVC r#nti*e c$lls $ controller $ction $n( p$sses te correct p$r$*eters to it.

    Te Controllerction)n!o0er cl$ss is te (ef$#lt $ction invo7er. Tis $ction invo7er #ses *o(elbin(ers to (eter*ine ow p$r$*eters $re p$sse( to $ctions.

    6o# the De"ault Model Binder Passes Parameters-n $ (ef$#lt MVC $pplic$tion+ tere is onl) one *o(el bin(er for te Controllerction)n!o0er to#se.Tis bin(er is $n inst$nce of te De"aultModelBinder cl$ss. Te (ef$#lt *o(el bin(er p$ssesp$r$*eters b) #sing te following logic8

    1. Te bin(er ex$*ines te (enition of te $ction t$t it *#st p$ss p$r$*eters to. -n teex$*ple+ te bin(er (eter*ines t$t te $ction re:#ires $n integer p$r$*eter c$lle(Photo)D.%. Te bin(er se$rces for v$l#es in te re:#est t$t c$n be p$sse( $s p$r$*eters. -n teex$*ple+ te bin(er se$rces for integers bec$#se te $ction re:#ires $n integer. Te bin(erse$rces for v$l#es in te following loc$tions in or(er8$. or* V$l#es8 -f te #ser lls o#t $ for* $n( clic7s $ s#b*it b#tton+ )o# c$n n( p$r$*eters inte Re1uest.Form collection.b.

  • 7/23/2019 Developing ASP.net MVC4 Applications

    18/22

    c. >#er) Strings8 -f te #ser re:#est incl#(es n$*e( p$r$*eters $fter $ :#estion *$r7+ )o# c$nn( tese p$r$*eters in te Re1uest./ueryString collection.(. iles8 -f te #ser re:#est incl#(es #plo$(e( les+ tese c$n be #se( $s p$r$*eters.

    Notice t$t if tere $re for* v$l#es $n( ro#te v$l#es in te re:#est+ for* v$l#es t$7e prece(ence.>#er) string v$l#es $re onl) #se( if tere $re no for* v$l#es $n( no ro#te v$l#es $v$il$ble $sp$r$*eters.

    Mo(el Extensibilit)Two w$)s in wic )o# c$n exten( te MVC $n(ling of MVC *o(els $re to cre$te c#sto* ($t$$nnot$tions $n( to cre$te c#sto* *o(el bin(ers.

    Custo% Validation Data Annotations"o# c$n #se ($t$ $nnot$tions to in(ic$te to MVC ow to v$li($te te ($t$ $ #ser enters in $ for*or p$sses in :#er) strings. Te fo#r b#ilt!in v$li($tion $ttrib#tes in MVC 4+Re1uired+ Range+String,ength+ $n( Regular'4$ression

    -n so*e sit#$tions+ s#c $s te following ex$*ples+ )o# *$) w$nt to r#n so*e c#sto* v$li($tionco(e8

    +#nning a Data Store Cec,. "o# w$nt to cec7 te ($t$ entere( $g$inst ($t$ t$t $s

    $lre$() been store( in te ($t$b$se or in $noter ($t$b$se store. Comparing -al#es. "o# w$nt to co*p$re two entere( v$l#es wit e$c oter.

    Matematical Cec,s. "o# w$nt to c$lc#l$te $ v$l#e fro* te entere( ($t$ $n( cec7 t$t

    v$l#e is v$li(.

    -n s#c sit#$tions )o# c$n cre$te $ c#sto* v$li($tion ($t$ $nnot$tion. To (o tis+ )o# cre$te $cl$ss t$t inerits fro* teSystem.Com$onentModel.Datannotations.Validationttributecl$ss.

    YAttrib#te0s$geAttrib#teT$rgets.iel(/Zp#blic cl$ss '$rgerT$nV$li($tionAttrib#te 8 V$li($tionAttrib#teJ p#blic int Mini*#*V$l#e J getX setX HHConstr#ctor

    p#blic '$rgerT$nV$li($tionAttrib#te int *ini*#*/ J Mini*#*V$l#e K *ini*#*X

    HH"o# *#st overri(e te -sV$li( *eto( to r#n )o#r test p#blic overri(e 5oole$n -sV$li( Ob,ect v$l#e/ J v$r v$l#eToCo*p$re K int/v$l#eX if v$l#eToCo*p$re F Mini*#*V$l#e/ J ret#rn tr#eX else J ret#rn f$lseX

    Y'$rgerT$nV$li($tionAttrib#te1@/Zp#blic VoterAge J getX setX

  • 7/23/2019 Developing ASP.net MVC4 Applications

    19/22

    Custo% Model BindersTe (ef$#lt *o(el bin(er is sopistic$te( $n( ^exible+ b#t so*eti*es )o# *$) w$nt to c#sto*i;eits be$vior to p$ss p$r$*eters in $n #n#s#$l w$). "o# c$n (o tis b) cre$ting $ c#sto* *o(elbin(er $n( registering it wit te MVC r#nti*e.

    Te following lines of co(e sow ow to cre$te $ si*ple c#sto* *o(el bin(er to p$ss p$r$*etersfro* te for* collection b) i*ple*enting te )ModelBinder interf$ce.

    p#blic cl$ss C$rMo(el5in(er 8 -Mo(el5in(erJ p#blic ob,ect 5in(Mo(el ControllerContext controllerContext+ Mo(el5in(ingContext bin(ingContext/ J HHIet te color for te c$r fro* te re:#est for* string color K controllerContext.&ttpContext.

  • 7/23/2019 Developing ASP.net MVC4 Applications

    20/22

    Data "ccess Logic. Tis co(e (enes te oper$tions necess$r) to persist ($t$ to $

    ($t$b$se. Tis incl#(es oper$tions to cre$te new recor(s+ re$( recor(s+ #p($te recor(s+$n( (elete recor(s in ($t$b$se t$bles. A single ob,ect in te b#siness logic l$)er *$) t$7e($t$ fro* *#ltiple ($t$b$se t$bles. Tis $bstr$ction is $n(le( b) te ($t$ $ccess logic.

    MVC (oes not re:#ire )o# to sep$r$te b#siness $n( ($t$ $ccess logic $n( )o# c$n cre$te MVC*o(el cl$sses t$t i*ple*ent bot l$)ers. Tis is often (one in s*$ll or si*ple web $pplic$tions

    wit s*$ll (evelop*ent te$*s. -n tese scen$rios+ Entit) r$*ewor7 cl$sses $re #se( (irectl) inte *o(el cl$sses.

    -n *ore co*plex sit#$tions+ )o# nee( to pl$ce te b#siness logic in MVC *o(el cl$sses $n( pl$cete ($t$ $ccess logic in (e(ic$te( cl$sses c$lle( repositories. Wen )o# t$7e tis $ppro$c+*o(el cl$sses $re in(epen(ent of te ($t$b$se str#ct#re $n( (o not incl#(e co(e t$t (epen(son ($t$b$se t$ble n$*es+ col#*n n$*es+ or view n$*es. Tis $ppro$c *$7es it e$sier tore(esign te ($t$b$se or *ove to $ (iGerent ($t$ store or ($t$ $ccess tecnolog)+ wito#t tenee( to reco(e )o#r entire $pplic$tion. 0sing tis $ppro$c+ )o# e*plo) te Entit) r$*ewor7 in)o#r repositor) cl$sses+ b#t not in )o#r *o(el cl$sses.

    6o# to Se$arate Model Classes and Re$ositories

    -f )o# (o coose to sep$r$te b#siness logic $n( ($t$ $ccess logic+ )o# *#st t$7e te followingsteps for e$c *o(el cl$ss81. ene $n interf$ce for te repositor) cl$ss. Tis interf$ce (ecl$res te *eto(s t$t terepositor) cl$ss #ses to re$( $n( write ($t$ fro* $n( to te ($t$b$se.%. Cre$te $n( write co(e for te repositor) cl$ss. Tis cl$ss *#st i*ple*ent $ll te ($t$ $ccess*eto(s (ecl$re( in te interf$ce.9.

  • 7/23/2019 Developing ASP.net MVC4 Applications

    21/22

    (he CommentsController Classp#blic cl$ss Co**entsController 8 ControllerJ-Co**ent

  • 7/23/2019 Developing ASP.net MVC4 Applications

    22/22

    4. eveloping ASP.NET MVC 4 ControllersWriting Controllers $n( Actions