ASP.net MVC 5 Explained | Computer Programming | Cyberspace

September 28, 2017 | Author: Anonymous | Category: ASP.NET
Share Embed


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 SECIFIC 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 $$liction. $$l iction.  %hile this is considered n introductory introducto ry course, this m&es  few ssum$tions. 'ou should h#e  good understnding understnding of C(, o")ect* oriented conce$ts, conce$ts, nd the wy the Internet wor&s in generl to fully understnd the wor&ings of n ASP.NET MVC $$liction. +et’s strt with the "sics of defining ASP.NET MVC.  What is ASP.NET MVC?  ASP.NET MVC is i s Microsoft’s Microsof t’s frmewor& frmewo r& for de#elo$ing de# elo$ing fst we" $$lictions $$lictions using their .NET $ltform with either the C( or V.NET lnguge. MVC is not  new conce$t nd ws introduced "c& in the -/0’s using  lnguge clled Smlltl&*/1. It ws lso im$lemented in the -20’s using Smlltl&*20. MVC is n cronym tht stnds for3 •





(M)odel (M)odel 4 5")ects tht hold your dt. (V)iew (V)iew 4 Views tht re $resented to your users, usully 6TM+ $ges or $ieces of 6TM+. (C)ontroller (C)ontroller 4 Controllers re wht orchestrtes the retrie#ing nd s#ing of dt, $o$ulting models, nd recei#ing dt from the users.

Microsoft hs t&en these conce$ts nd integrted them into  fle7i"le nd esy we" frmewor&, clling it ASP.NET MVC. 8or those unwre of ASP.NET, "elow is  list of terms to understnd the technology we’ll "e using in this tutoril.







.NET Framework  4  4 A technology introduced in 9009 which includes the "ility to crete e7ecut"les, we" $$lictions, nd ser#ices using C( :$ronounced see*shr$;, Visul sic, nd 8(.  ASP.NET 4  ASP.NET  4 An o$en*source ser#er*side we" $$liction frmewor&   which is  su"set su"s et of Microsoft’s Micro soft’s .NET frmewor&. frmewo r&. Their first fi rst itertion of ASP.NET included  technology clled %e" 8orms.  ASP.NET WebForms WebForm s 4 :9009 4 current; A $ro$rietry $ro$rietry techni!ue de#elo$ed "y Microsoft to mnge stte nd form dt cross multi$le $ges.

!hy "# "t #o $o$%lar& Since ASP.NET MVC $$ered eight yers go, it hs "een  "reth 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 forwrd with MVC. ut why hs it "ecome so $o$ulr< •







 ASP.NET MVC has h as a separation separati on of onerns. onern s. Se$rtion Se$rtion of concerns mens tht your "usiness logic is not contined in  View or controller. The "usiness logic should "e found in the models of your $$liction. This m&es we" de#elo$ment e#en esier "ecuse it llows you to focus on integrting 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 tht 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 $$liction, the more dur"le your $$liction 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 clled ViewStte ViewSt te tht trc&s ll of the controls on  $ge. If you h#e  ton of controls on  your %e"8orm, %e"8orm , the ViewStte ViewStt e cn grow to "ecome n issue. issu e.  ASP.NET MVC doesn’t do esn’t h#e  ViewStte, V iewStte, thus m&ing the View V iew len nd men.  ASP.NET MVC has h as more ontro# ontr o# o!er TM*. Since ser#er*side controls ren’t used, the View cn "e s smll s  you wnt it to "e. It $ro#ides $ro# ides  "etter grnulrity grnulrit y nd control o#er how you wnt your $ges rendered in the "rowser.







.NET Framework  4  4 A technology introduced in 9009 which includes the "ility to crete e7ecut"les, we" $$lictions, nd ser#ices using C( :$ronounced see*shr$;, Visul sic, nd 8(.  ASP.NET 4  ASP.NET  4 An o$en*source ser#er*side we" $$liction frmewor&   which is  su"set su"s et of Microsoft’s Micro soft’s .NET frmewor&. frmewo r&. Their first fi rst itertion of ASP.NET included  technology clled %e" 8orms.  ASP.NET WebForms WebForm s 4 :9009 4 current; A $ro$rietry $ro$rietry techni!ue de#elo$ed "y Microsoft to mnge stte nd form dt cross multi$le $ges.

!hy "# "t #o $o$%lar& Since ASP.NET MVC $$ered eight yers go, it hs "een  "reth 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 forwrd with MVC. ut why hs it "ecome so $o$ulr< •







 ASP.NET MVC has h as a separation separati on of onerns. onern s. Se$rtion Se$rtion of concerns mens tht your "usiness logic is not contined in  View or controller. The "usiness logic should "e found in the models of your $$liction. This m&es we" de#elo$ment e#en esier "ecuse it llows you to focus on integrting 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 tht 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 $$liction, the more dur"le your $$liction 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 clled ViewStte ViewSt te tht trc&s ll of the controls on  $ge. If you h#e  ton of controls on  your %e"8orm, %e"8orm , the ViewStte ViewStt e cn grow to "ecome n issue. issu e.  ASP.NET MVC doesn’t do esn’t h#e  ViewStte, V iewStte, thus m&ing the View V iew len nd men.  ASP.NET MVC has h as more ontro# ontr o# o!er TM*. Since ser#er*side controls ren’t used, the View cn "e s smll s  you wnt it to "e. It $ro#ides $ro# ides  "etter grnulrity grnulrit y nd control o#er how you wnt your $ges rendered in the "rowser.

There re  num"er of other resons, "ut  lot of thought ws $ut into  ASP.NET MVC nd o#er eight yers, it hs mtured into int o n e7ce$tionl frmewor& for we" de#elo$ers.

'e(%"rement# and "n#tallat"on To strt "uilding ASP.NET ASP.NET MVC $$lictions, $$lictions, it is recommended recommended tht you h#e the following3 •



 A #ersion of o f Visul Studio St udio :$refer"ly :$ref er"ly 90-=, 90->, 9 0->, or Community Com munity Edition; instlled on your mchine. The Community Edition cn "e found thtt$3??www.#isulstudio.com? thtt$3??www.#isulstudio.com?.. .NET 8rmewor& :@.0 or higher;

5nce you’#e instlled Visul Studio, we cn crete  sm$le $ro)ect to find out wht’s included with n MVC $ro)ect.

Layo%t of an M)C $ro*e+t I &now you wnt to )um$ right into de#elo$ment, de#elo$ment, "ut you h#e to understnd where e#erything e#erything is t "efore you strt coding.  'ou need to &now where the hmmer h mmer nd screwdri#er screwdr i#er re "efore "efor e "uilding, right<  %hen you crete cret 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 ment to hold dt for your $$liction :)ust s the nme sys;. A cou$le of e7m$les would include  $ort"le dt"se :li&e S+ Ser#er Com$ct Edition; or ny &ind of dt files :BM+, S5N, etc.;. I $refer to use S+ Ser#er.  App+Start 4 The A$$DStrt folder contins the initilition nd configurtion of different fetures of your $$liction. •



undleConfig.cs 4 This contins ll of the configurtion for minifying nd com$ressing your #Scri$t nd CSS files into one file. 8ilterConfig.cs 4 Fegisters Glo"l 8ilters.



FouteConfig.cs 4 Configurtion of your routes.

There re other 7777Config.cs files tht re dded when you $$ly other MVC*relted technologies :for e7m$le, %e"API dds %e"A$iConfig.cs;. Content 4 This folder is ment for ll of your sttic content li&e imges nd style sheets. It’s "est to crete folders for them li&e Himges or Hstyles or Hcss. Contro##ers 4 The controllers folder is where we $lce the controllers. Mo"e#s 4 This folder contins your "usiness models. It’s "etter when you h#e these models in nother $ro)ect, "ut for demo $ur$oses, we’ll $lce them in here. Sripts 4 This is where your #Scri$t scri$ts reside.  Views 4 This $rent folder contins ll of your 6TM+ HViews with ech controller nme s  folder. Ech folder will contin  num"er of cshtml files relting to the methods in tht folder’s controller. If we hd  JF+ tht 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 Shred folder is ment for ny shred cshtml files  you need cross the we"site. #oba#.asa& 4 The Glo"l.s7 is ment for the initilition of the we" $$liction. If you loo& inside the Glo"l.s7, you’ll notice tht this is  where the FouteConfig.cs, undleConfig.cs, nd 8ilterConfig.cs re clled  when the $$liction runs.  Web.Confi/ 4 The we".config is where you $lce configurtion settings for your $$liction. 8or new MVC de#elo$ers, it’s good to &now tht you cn $lce settings inside the K$$settingsL tg nd $lce connection strings inside the KconnectionstringL tg. Now tht you &now where e#erything is locted in your $ro)ect, we cn mo#e forwrd with wht is the $rocess when n MVC $$liction is initilly clled.

,eneral -o. of an M)C a$$l"+at"on

The flow of n MVC $$liction cn get confusing, "ut I wnt to #isully 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" $$liction is clled, Foute o")ects re dded to the FouteT"le o")ect. 5nce the route t"le is creted,  we "uild dditionl routing o")ects nd $erform the routing using the FouteT"le, which cretes the Foutet, then the Fe!uestConte7t. @. Now tht we h#e our Fe!uest Conte7t, the M#cFoute6ndler cretes n M#c6ndler. The M#c6ndler is wht &ic&s off the strt of n MVC A$$liction. It lso identifies which controller to use  "sed on the 6TTP re!uest. The M#c6ndler cretes the controller nd clls 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 tht method. 1. The ction method is e7ecuted while $ssing in user $rmeters, orchestrtes res$onse dt, nd returns  resulting ActionFesult. Jsully,  ViewFesult is returned "y MVC. /. The dt is $ssed through to either the defult View or  s$ecified different View through  ViewFesult. The dt is lid into the View :6TM+; using For $lceholders. Thin& of it s  tem$lte. 2. 5nce the dt is in the View, it now $rocesses the View, $erforming re$lcements "y e7ecuting code resulting in $lin 6TM+. . The finl 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 esy the $rocess is while we wor& through this tutoril. +et’s strt with the "sics.

Under#tand"n/ the e##ent"al# Model# What are Models?  Models re $ro""ly the esiest section to ddress first. Models re the o")ects tht define nd store your dt so you cn use them throughout the $$liction. I would consider Models to "e the e!ui#lent of $lin old C+F :Common +nguge Funtime; o")ects, or P5C5’s. A P5C5 is  $lin clss tht holds structured dt. Tht’s it. 8or e7m$le, if you hd  "usiness, one sim$le P5C5 :or model; would "e similr to3

 public class Customer {  public string FirstName { get; set; }

 public string LastName { get; set; }  public string Company { get; set; }  public Inumera!"e#$rder% $rders { get; set; } }

5f course, you do h#e o$tions when dding dditionl functionlity to  your P5C5’s. 8or e7m$le, how long h#e they "een with you s  customer<  'our "usiness Model my loo& something li&e this3

 public class Customer {  public string FirstName { get; set; }  public string LastName { get; set; }  public string Company { get; set; }  public Inumera!"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 e7m$le of wht  model is, this won’t "e the lst time we discuss models.

Controller# Controllers re considered to "e the coordintors or he#y lifters of your $$liction.  %hen  re!uest comes in for  we" $ge :GET;, it goes through  controller. If  user hits  su"mit "utton :P5ST;, tht dt is sent "c& to  controller. 'ou cn e#en use controllers to return smller $ortions of 6TM+.  A controller hs 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 my notice tht the nme of our clss is clled E7m$leController. This is  con#ention in MVC. All controllers re re!uired to h#e  suffi7 of Hcontroller. It lso m&es it esy to s$ot when "uilding your JF+’s. In the e7m$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 efult.s$7 on ech controller. The Inde7 is ssumed nd utomticlly 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 ech method, you’ll notice  H return

1iew)*; .

%ht is this<

This View method is clled n ActionFesult. ActionFesults re  wy to tell the controller wht to return to  View or wht to return to the "rowser. Essentilly, ActionFesults re wht you wnt to return when you re done $rocessing your Action method. %e’ll tl& "out  num"er of  ActionFesults lter in the tutoril. In this cse of the E7m$leController "o#e, no dt is sent to the Inde7  #iew. If you o"ser#e  method returning n ActionFesult without ny dt :li&e View:;;, it’s sfe to sy tht  model isn’t ttched to the View.

 %hen you see the View:; returned, wht is ctully h$$ening "ehind the scenes is tht the controller cretes  new ViewFesult nd returns tht s the result. The View method is )ust  wy to cmouflge the under$innings of wht m&es  ViewFesult function $ro$erly.  'ou could #ery esily 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 frmewor& h#e mde this e7tremely esy to use "y creting  View:; method t the controller le#el. The returning of the View:; with the defult method sim$lifies the $rocess. Most de#elo$ers new to MVC h#e s&ed the !uestion, H%ht’s the difference "etween  ViewFesult nd n ActionFesult
View more...

Comments

Copyright © 2017 DATENPDF Inc.