ASP.net MVC 5 Explained | Computer Programming | Cyberspace
Short Description
ASP.net MVC 5 Explained - Download as Word Doc (.doc / .docx), PDF File (.pdf), Text File (.txt) or read online. ASP.net...
Description
By Jonathan By Jonathan Danylko Danyl ko for Udemy
Interested in more than th an just a guide? guide ? Check out a full course. cour se. TABLE OF CONTENTS: CLICK TO JUM TO A SECIFIC SECTION
In this introduction to Microsoft’s ASP.NET MVC, we will discuss the technology, terminology, nd techni!ues techni!ues for "uilding your #ery first ASP.NET MVC $$liction. $$l iction. %hile this is considered n introductory introducto ry course, this m&es few ssum$tions. 'ou should h#e good understnding understnding of C(, o")ect* oriented conce$ts, conce$ts, nd the wy the Internet wor&s in generl to fully understnd the wor&ings of n ASP.NET MVC $$liction. +et’s strt with the "sics of defining ASP.NET MVC. What is ASP.NET MVC? ASP.NET MVC is i s Microsoft’s Microsof t’s frmewor& frmewo r& for de#elo$ing de# elo$ing fst we" $$lictions $$lictions using their .NET $ltform with either the C( or V.NET lnguge. MVC is not new conce$t nd ws introduced "c& in the -/0’s using lnguge clled Smlltl&*/1. It ws lso im$lemented in the -20’s using Smlltl&*20. MVC is n cronym tht stnds for3 •
•
•
(M)odel (M)odel 4 5")ects tht hold your dt. (V)iew (V)iew 4 Views tht re $resented to your users, usully 6TM+ $ges or $ieces of 6TM+. (C)ontroller (C)ontroller 4 Controllers re wht orchestrtes the retrie#ing nd s#ing of dt, $o$ulting models, nd recei#ing dt from the users.
Microsoft hs t&en these conce$ts nd integrted them into fle7i"le nd esy we" frmewor&, clling it ASP.NET MVC. 8or those unwre of ASP.NET, "elow is list of terms to understnd the technology we’ll "e using in this tutoril.
•
•
•
.NET Framework 4 4 A technology introduced in 9009 which includes the "ility to crete e7ecut"les, we" $$lictions, nd ser#ices using C( :$ronounced see*shr$;, Visul sic, nd 8(. ASP.NET 4 ASP.NET 4 An o$en*source ser#er*side we" $$liction frmewor& which is su"set su"s et of Microsoft’s Micro soft’s .NET frmewor&. frmewo r&. Their first fi rst itertion of ASP.NET included technology clled %e" 8orms. ASP.NET WebForms WebForm s 4 :9009 4 current; A $ro$rietry $ro$rietry techni!ue de#elo$ed "y Microsoft to mnge stte nd form dt cross multi$le $ges.
!hy "# "t #o $o$%lar& Since ASP.NET MVC $$ered eight yers go, it hs "een "reth of fresh ir for Microsoft de#elo$ers. de#elo$ers. Most de#elo$ers sto$$ed wor&ing with %e"8orms to mo#e forwrd with MVC. ut why hs it "ecome so $o$ulr< •
•
•
•
ASP.NET MVC has h as a separation separati on of onerns. onern s. Se$rtion Se$rtion of concerns mens tht your "usiness logic is not contined in View or controller. The "usiness logic should "e found in the models of your $$liction. This m&es we" de#elo$ment e#en esier "ecuse it llows you to focus on integrting your "usiness rules into reus"le models. ASP.NET MVC pro!i"es p ro!i"es testabi#it$ testab i#it$ o%t of the bo&. Another selling sel ling $oint is tht ASP.NET MVC llows you to test tes t e#ery single one of your com$onents, com$onents, there"y m&ing your code lmost "ullet$roof. "ullet$roo f. The more unit u nit tests you $ro#ide $ro#i de for your $$liction, the more dur"le your $$liction will "ecome. ASP.NET MVC has h as a sma##er 'View footprint. fo otprint. %ith %e"8orms, %e"8orm s, there is ser#er #ri"le #ri"l e clled ViewStte ViewSt te tht trc&s ll of the controls on $ge. If you h#e ton of controls on your %e"8orm, %e"8orm , the ViewStte ViewStt e cn grow to "ecome n issue. issu e. ASP.NET MVC doesn’t do esn’t h#e ViewStte, V iewStte, thus m&ing the View V iew len nd men. ASP.NET MVC has h as more ontro# ontr o# o!er TM*. Since ser#er*side controls ren’t used, the View cn "e s smll s you wnt it to "e. It $ro#ides $ro# ides "etter grnulrity grnulrit y nd control o#er how you wnt your $ges rendered in the "rowser.
•
•
•
.NET Framework 4 4 A technology introduced in 9009 which includes the "ility to crete e7ecut"les, we" $$lictions, nd ser#ices using C( :$ronounced see*shr$;, Visul sic, nd 8(. ASP.NET 4 ASP.NET 4 An o$en*source ser#er*side we" $$liction frmewor& which is su"set su"s et of Microsoft’s Micro soft’s .NET frmewor&. frmewo r&. Their first fi rst itertion of ASP.NET included technology clled %e" 8orms. ASP.NET WebForms WebForm s 4 :9009 4 current; A $ro$rietry $ro$rietry techni!ue de#elo$ed "y Microsoft to mnge stte nd form dt cross multi$le $ges.
!hy "# "t #o $o$%lar& Since ASP.NET MVC $$ered eight yers go, it hs "een "reth of fresh ir for Microsoft de#elo$ers. de#elo$ers. Most de#elo$ers sto$$ed wor&ing with %e"8orms to mo#e forwrd with MVC. ut why hs it "ecome so $o$ulr< •
•
•
•
ASP.NET MVC has h as a separation separati on of onerns. onern s. Se$rtion Se$rtion of concerns mens tht your "usiness logic is not contined in View or controller. The "usiness logic should "e found in the models of your $$liction. This m&es we" de#elo$ment e#en esier "ecuse it llows you to focus on integrting your "usiness rules into reus"le models. ASP.NET MVC pro!i"es p ro!i"es testabi#it$ testab i#it$ o%t of the bo&. Another selling sel ling $oint is tht ASP.NET MVC llows you to test tes t e#ery single one of your com$onents, com$onents, there"y m&ing your code lmost "ullet$roof. "ullet$roo f. The more unit u nit tests you $ro#ide $ro#i de for your $$liction, the more dur"le your $$liction will "ecome. ASP.NET MVC has h as a sma##er 'View footprint. fo otprint. %ith %e"8orms, %e"8orm s, there is ser#er #ri"le #ri"l e clled ViewStte ViewSt te tht trc&s ll of the controls on $ge. If you h#e ton of controls on your %e"8orm, %e"8orm , the ViewStte ViewStt e cn grow to "ecome n issue. issu e. ASP.NET MVC doesn’t do esn’t h#e ViewStte, V iewStte, thus m&ing the View V iew len nd men. ASP.NET MVC has h as more ontro# ontr o# o!er TM*. Since ser#er*side controls ren’t used, the View cn "e s smll s you wnt it to "e. It $ro#ides $ro# ides "etter grnulrity grnulrit y nd control o#er how you wnt your $ges rendered in the "rowser.
There re num"er of other resons, "ut lot of thought ws $ut into ASP.NET MVC nd o#er eight yers, it hs mtured into int o n e7ce$tionl frmewor& for we" de#elo$ers.
'e(%"rement# and "n#tallat"on To strt "uilding ASP.NET ASP.NET MVC $$lictions, $$lictions, it is recommended recommended tht you h#e the following3 •
•
A #ersion of o f Visul Studio St udio :$refer"ly :$ref er"ly 90-=, 90->, 9 0->, or Community Com munity Edition; instlled on your mchine. The Community Edition cn "e found thtt$3??www.#isulstudio.com? thtt$3??www.#isulstudio.com?.. .NET 8rmewor& :@.0 or higher;
5nce you’#e instlled Visul Studio, we cn crete sm$le $ro)ect to find out wht’s included with n MVC $ro)ect.
Layo%t of an M)C $ro*e+t I &now you wnt to )um$ right into de#elo$ment, de#elo$ment, "ut you h#e to understnd where e#erything e#erything is t "efore you strt coding. 'ou need to &now where the hmmer h mmer nd screwdri#er screwdr i#er re "efore "efor e "uilding, right< %hen you crete cret e new MVC $ro)ect, $ro) ect, your solution so lution should s hould h#e the th e following structure in your Solution E7$lorer.
App+,ata 4 %hile I don’t use this folder often, it’s ment to hold dt for your $$liction :)ust s the nme sys;. A cou$le of e7m$les would include $ort"le dt"se :li&e S+ Ser#er Com$ct Edition; or ny &ind of dt files :BM+, S5N, etc.;. I $refer to use S+ Ser#er. App+Start 4 The A$$DStrt folder contins the initilition nd configurtion of different fetures of your $$liction. •
•
undleConfig.cs 4 This contins ll of the configurtion for minifying nd com$ressing your #Scri$t nd CSS files into one file. 8ilterConfig.cs 4 Fegisters Glo"l 8ilters.
•
FouteConfig.cs 4 Configurtion of your routes.
There re other 7777Config.cs files tht re dded when you $$ly other MVC*relted technologies :for e7m$le, %e"API dds %e"A$iConfig.cs;. Content 4 This folder is ment for ll of your sttic content li&e imges nd style sheets. It’s "est to crete folders for them li&e Himges or Hstyles or Hcss. Contro##ers 4 The controllers folder is where we $lce the controllers. Mo"e#s 4 This folder contins your "usiness models. It’s "etter when you h#e these models in nother $ro)ect, "ut for demo $ur$oses, we’ll $lce them in here. Sripts 4 This is where your #Scri$t scri$ts reside. Views 4 This $rent folder contins ll of your 6TM+ HViews with ech controller nme s folder. Ech folder will contin num"er of cshtml files relting to the methods in tht folder’s controller. If we hd JF+ tht loo&ed li&e this3 http://www.xyzcompany.com/Products/List
we would h#e Products folder with +ist.cshtml file. %e would lso &now to loo& in the Controllers folder nd o$en the ProductsController.cs nd loo& for the +ist method. Views-Share" 4 The Shred folder is ment for ny shred cshtml files you need cross the we"site. #oba#.asa& 4 The Glo"l.s7 is ment for the initilition of the we" $$liction. If you loo& inside the Glo"l.s7, you’ll notice tht this is where the FouteConfig.cs, undleConfig.cs, nd 8ilterConfig.cs re clled when the $$liction runs. Web.Confi/ 4 The we".config is where you $lce configurtion settings for your $$liction. 8or new MVC de#elo$ers, it’s good to &now tht you cn $lce settings inside the K$$settingsL tg nd $lce connection strings inside the KconnectionstringL tg. Now tht you &now where e#erything is locted in your $ro)ect, we cn mo#e forwrd with wht is the $rocess when n MVC $$liction is initilly clled.
,eneral -o. of an M)C a$$l"+at"on
The flow of n MVC $$liction cn get confusing, "ut I wnt to #isully show you how sim$le re!uest mo#es through the ASP.NET $i$eline. -. A user re!uests we" $ge using "rowser. 9. The ser#er recei#es the re!uest. =. If this is the first time the we" $$liction is clled, Foute o")ects re dded to the FouteT"le o")ect. 5nce the route t"le is creted, we "uild dditionl routing o")ects nd $erform the routing using the FouteT"le, which cretes the Foutet, then the Fe!uestConte7t. @. Now tht we h#e our Fe!uest Conte7t, the M#cFoute6ndler cretes n M#c6ndler. The M#c6ndler is wht &ic&s off the strt of n MVC A$$liction. It lso identifies which controller to use "sed on the 6TTP re!uest. The M#c6ndler cretes the controller nd clls the e7ecute method. >. 5nce we h#e our Controller identified nd selected, we need to find the method to e7ecute. This is done "y using the ControllerActionIn#o&er. 5nce found, it e7ecutes tht method. 1. The ction method is e7ecuted while $ssing in user $rmeters, orchestrtes res$onse dt, nd returns resulting ActionFesult. Jsully, ViewFesult is returned "y MVC. /. The dt is $ssed through to either the defult View or s$ecified different View through ViewFesult. The dt is lid into the View :6TM+; using For $lceholders. Thin& of it s tem$lte. 2. 5nce the dt is in the View, it now $rocesses the View, $erforming re$lcements "y e7ecuting code resulting in $lin 6TM+. . The finl View is sent "c& to the user in their "rowser.
If this seems li&e lot to remem"er, it’s o&y. 'ou’ll see how esy the $rocess is while we wor& through this tutoril. +et’s strt with the "sics.
Under#tand"n/ the e##ent"al# Model# What are Models? Models re $ro""ly the esiest section to ddress first. Models re the o")ects tht define nd store your dt so you cn use them throughout the $$liction. I would consider Models to "e the e!ui#lent of $lin old C+F :Common +nguge Funtime; o")ects, or P5C5’s. A P5C5 is $lin clss tht holds structured dt. Tht’s it. 8or e7m$le, if you hd "usiness, one sim$le P5C5 :or model; would "e similr to3
public class Customer { public string FirstName { get; set; }
public string LastName { get; set; } public string Company { get; set; } public Inumera!"e#$rder% $rders { get; set; } }
5f course, you do h#e o$tions when dding dditionl functionlity to your P5C5’s. 8or e7m$le, how long h#e they "een with you s customer< 'our "usiness Model my loo& something li&e this3
public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Company { get; set; } public Inumera!"e#$rder% $rders { get; set; } public &ate'ime First(et { get; set; }
public int CalculateAge)&ate'ime end'ime* { var a+e , end'ime.-ear First(et.-ear; if )end'ime # First(et.dd-ears)a+e** a+e; return a+e; }
}
%hile this is #ery sim$le e7m$le of wht model is, this won’t "e the lst time we discuss models.
Controller# Controllers re considered to "e the coordintors or he#y lifters of your $$liction. %hen re!uest comes in for we" $ge :GET;, it goes through controller. If user hits su"mit "utton :P5ST;, tht dt is sent "c& to controller. 'ou cn e#en use controllers to return smller $ortions of 6TM+. A controller hs the following structure3
public class ExampleController : Controller
{ public ction0esu"t Index)* { return 1iew)*; }
23ttpPost4 public ction0esu"t Index)xamp"e1iew(ode" mode"* { return 1iew)*; } }
0MP12TANT3 'ou my notice tht the nme of our clss is clled E7m$leController. This is con#ention in MVC. All controllers re re!uired to h#e suffi7 of Hcontroller. It lso m&es it esy to s$ot when "uilding your JF+’s. In the e7m$le "o#e, our JF+ would "e3 http://www.xyzcompany.com/xamp"e/Index
or http://www.xyzcompany.com/xamp"e
In %e"8orm s$e&*ese, the Inde7 method is the e!ui#lent to the efult.s$7 on ech controller. The Inde7 is ssumed nd utomticlly dded to the end of the JF+, so "oth JF+’s end u$ going to the Inde7 method. A+t"on'e#%lt#
At the end of ech method, you’ll notice H return
1iew)*; .
%ht is this<
This View method is clled n ActionFesult. ActionFesults re wy to tell the controller wht to return to View or wht to return to the "rowser. Essentilly, ActionFesults re wht you wnt to return when you re done $rocessing your Action method. %e’ll tl& "out num"er of ActionFesults lter in the tutoril. In this cse of the E7m$leController "o#e, no dt is sent to the Inde7 #iew. If you o"ser#e method returning n ActionFesult without ny dt :li&e View:;;, it’s sfe to sy tht model isn’t ttched to the View.
%hen you see the View:; returned, wht is ctully h$$ening "ehind the scenes is tht the controller cretes new ViewFesult nd returns tht s the result. The View method is )ust wy to cmouflge the under$innings of wht m&es ViewFesult function $ro$erly. 'ou could #ery esily do something li&e this3 public ction0esu"t Index)* { return new 1iew0esu"t { 1iewName , 5Index5 };
}
ut the de#elo$ers of the ASP.NET MVC frmewor& h#e mde this e7tremely esy to use "y creting View:; method t the controller le#el. The returning of the View:; with the defult method sim$lifies the $rocess. Most de#elo$ers new to MVC h#e s&ed the !uestion, H%ht’s the difference "etween ViewFesult nd n ActionFesult
View more...
Comments