毕设内容MVC英文论文附带翻译

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1ASP.NETMVCIn-Depth:TheLifeofanASP.NETMVCRequestThepurposeofthisblogentryistodescribe,inpainfuldetail,eachstepinthelifeofanASP.NETMVCrequestfrombirthtodeath.IwanttounderstandeverythingthathappenswhenyoutypeaURLinabrowserandhittheenterkeywhenrequestingapagefromanASP.NETMVCwebsite.WhydoIcare?Therearetworeasons.First,oneofthepromisesofASP.NETMVCisthatitwillbeaveryextensibleframework.Forexample,you’llbeabletoplugindifferentviewenginestocontrolhowyourwebsitecontentisrendered.Youalsowillbeabletomanipulatehowcontrollersgetgeneratedandassignedtoparticularrequests.IwanttowalkthroughthestepsinvolvedinanASP.NETMVCpagerequestbecauseIwanttodiscoveranyandalloftheseextensibilitypoints.Second,I’minterestedinTest-DrivenDevelopment.Inordertowriteunittestsforcontrollers,Ineedtounderstandallofthecontrollerdependencies.Whenwritingmytests,IneedtomockcertainobjectsusingamockingframeworksuchasTypemockIsolatororRhinoMocks.IfIdon’tunderstandthepagerequestlifecycle,Iwon’tbeabletoeffectivelymockit.TwoWarningsButfirst,twowarnings.Here'sthefirstwarning:I’mwritingthisblogentryaweekaftertheASP.NETMVCPreview2waspubliclyreleased.TheASP.NETMVCframeworkisstillverymuchinBeta.Therefore,anythingthatIdescribeinthisblogentrymightbeoutdatedand,therefore,wronginacoupleofmonths.So,ifyouarereadingthisblogentryafterMay2008,don’tbelieveeverythingyouread.Second,thisblogentryisnotmeantasanoverviewofASP.NETMVC.IdescribethelifecycleofanASP.NETMVCrequestinexcruciatinganddifficulttoreaddetail.Okay,youhavebeenwarned.OverviewoftheLifecycleStepsTherearefivemainstepsthathappenwhenyoumakearequestfromanASP.NETMVCwebsite:1.Step1–TheRouteTableisCreatedThisfirststephappensonlyoncewhenanASP.NETapplicationfirststarts.TheRouteTablemapsURLstohandlers.2.Step2–TheUrlRoutingModuleInterceptstheRequestThissecondstephappenswheneveryoumakearequest.TheUrlRoutingModuleinterceptseveryrequestandcreatesandexecutestherighthandler.3.Step3–TheMvcHandlerExecutesTheMvcHandlercreatesacontroller,passesthecontrolleraControllerContext,andexecutesthecontroller.4.Step4–TheControllerExecutesThecontrollerdetermineswhichcontrollermethodtoexecute,buildsalistofparameters,andexecutesthemethod.5.Step5–TheRenderViewMethodisCalledTypically,acontrollermethodcallsRenderView()torendercontentbacktothebrowser.TheController.RenderView()methoddelegatesitsworktoaparticularViewEngine.Let’sexamineeachofthesestepsindetail.Step1:TheRouteTableisCreatedWhenyourequestapagefromanormalASP.NETapplication,thereisapageondiskthatcorrespondstoeachpagerequest.Forexample,ifyourequestapagenamedSomePage.aspxthen2therebetterbeapagenamedSomePage.aspxsittingonyourwebserver.Ifnot,youreceiveanerror.Technically,anASP.NETpagerepresentsaclass.And,notjustanyclass.AnASP.NETpageisahandler.Inotherwords,anASP.NETpageimplementstheIHttpHandlerinterfaceandhasaProcessRequest()methodthatgetscalledwhenyourequestthepage.TheProcessRequest()methodisresponsibleforgeneratingthecontentthatgetssentbacktothebrowser.So,thewaythatanormalASP.NETapplicationworksissimpleandintuitive.Yourequestapage,thepagerequestcorrespondstoapageondisk,thepageexecutesitsProcessRequest()methodandcontentgetssentbacktothebrowser.AnASP.NETMVCapplicationdoesnotworklikethis.WhenyourequestapagefromanASP.NETMVCapplication,thereisnopageondiskthatcorrespondstotherequest.Instead,therequestisroutedtoaspecialclasscalledacontroller.Thecontrollerisresponsibleforgeneratingthecontentthatgetssentbacktothebrowser.WhenyouwriteanormalASP.NETapplication,youbuildabunchofpages.Thereisalwaysaone-to-onemappingbetweenURLsandpages.Correspondingtoeachpagerequest,therebetterbeapage.WhenyoubuildanASP.NETMVCapplication,incontrast,youbuildabunchofcontrollers.Theadvantageofusingcontrollersisthatyoucanhaveamany-to-onemappingbetweenURLsandpages.Forexample,allofthefollowingURLscanbemappedtothesamecontroller:ThesinglecontrollermappedtotheseURLscandisplayproductinformationfortherightproductbyextractingtheproductIdfromtheURL.ThecontrollerapproachismoreflexiblethantheclassicASP.NETapproach.ThecontrollerapproachalsoresultsinmorereadableandintuitiveURLs.So,howdoesaparticularpagerequestgetroutedtoaparticularcontroller?AnASP.NETMVCapplicationhassomethingcalledaRouteTable.TheRouteTablemapsparticularURLstoparticularcontrollers.AnapplicationhasoneandonlyoneRouteTable.ThisRouteTableissetupintheGlobal.asaxfile.Listing1containsthedefaultGlobal.asaxfilethatyougetwhenyoucreateanewASP.NETMVCWebApplicationprojectbyusingVisualStudio.Anapplication’sRouteTableisrepresentedbythestaticRouteTable.Routesproperty.ThispropertyrepresentsacollectionofRouteobjects.IntheGlobal.asaxfileinListing1,twoRouteobjectsareaddedtotheRouteTablewhentheapplicationfirststarts(TheApplication_Start()methodiscalledonlyoncewhentheveryfirstpageisrequestedfromawebsite).ARouteobjectisresponsibleformappingURLstohandlers.InListing1,twoRouteobjectsarecreated.BothRouteobjectsmapURLstotheMvcRouteHandler.ThefirstRoutemapsanyURLthatfollowsthepattern{controller}/{action}/{id}totheMvcRouteHandler.ThesecondRoutemapstheparticularURLDefault.aspxtotheMvcRouteHandler.Bytheway,

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功