Anda di halaman 1dari 5

1) MVC page Life Cycle?

There are seven main steps that happen when you make a request to an Aspnet MVC we!
applications "or more #etails refer $etaile# A%&'(T MVC &ipeline
1 )outing
Aspnet )outing is the *rst step in MVC request cycle +asically it is a pattern matching
system that matches the request,s -)L against the registere# -)L patterns in the )oute
Ta!le .hen a matching pattern foun# in the )oute Ta!le/ the )outing engine forwar#s the
request to the correspon#ing 0)oute1an#ler for that request The #efault one calls
the MvcHandler The routing engine returns a 232 1TT& status co#e against that request if
the patterns is not foun# in the )oute Ta!le
.hen application starts at *rst time/ it registers one or more patterns to the )oute Ta!le to
tell the routing system what to #o with any requests that match these patterns An
application has only one )oute Ta!le an# this is setup in the 4lo!alasa5 *le of the
application
1. public static void RegisterRoutes(RouteCollection routes)
2. {
3. routes.IgnoreRoute("{resource}.axd/{pat!In"o}")#
$. routes.%apRoute( "&e"ault"' // Route name
(. "{controller}/{action}/{id}"' // URL with parameters
). ne* { controller + ",o-e"' action + "Index"' id +
.rl/ara-eter.0ptional } // Parameter defaults
1. )#
2. }
6 Mvc1an#ler
The Mvc1an#ler is responsi!le for initiating the real processing insi#e A%&'(T MVC MVC
han#ler implements 01ttp1an#ler interface an# further process the request !y
using ProcessRequest metho# as shown !elow7
1. protected internal virtual void /rocessRe3uest(,ttpContext4ase
!ttpContext)
2. {
3. 5ecurit6.til./rocessIn7pplication8rust(delegate {
$. IController controller#
(. IController9actor6 "actor6#
). t!is./rocessRe3uestInit(!ttpContext' out controller' out "actor6)#
1. tr6
2. {
:. controller.;xecute(t!is.Re3uestContext)#
1<. }
11. "inall6
12. {
13. "actor6.ReleaseController(controller)#
1$. }
1(. })#
1). }
8 Controller
As shown in a!ove co#e/ Mvc1an#ler uses the 0Controller"actory instance an# tries to get a
0Controller instance 0f successful/ the (5ecute metho# is calle# The 0Controller"actory coul#
!e the #efault controller factory or a custom factory initiali9e# at
the Application_Start event/ as shown !elow7
1. protected void 7pplication=5tart()
2. {
3. 7reaRegistration.Register7ll7reas()#
$. RegisterRoutes(Route8able.Routes)#
(. Controller4uilder.Current.5etController9actor6(ne*
Custo-Controller9actor6())#
). }
2 Action (5ecution
:nce the controller has !een instantiate#/ Controller;s Action0nvoker #etermines which
speci*c action to invoke on the controller Action to !e e5ecute is chosen !ase# on
attri!utes ActionNameSelectorAttribute <!y #efault metho# which have the same name
as the action is chosen) an# ActionMethodSelectorAttribute<0f more than one metho#
foun#/ the correct one is chosen with the help of this attri!ute)
= View )esult
The action metho# receives user input/ prepares the appropriate response #ata/ an# then
e5ecutes the result !y returning a result type The result type can !e View)esult/
)e#irectTo)oute)esult/ )e#irect)esult/ Content)esult/ >son)esult/ "ile)esult/ an#
(mpty)esult
? View (ngine
The *rst step in the e5ecution of the View )esult involves the selection of the appropriate
View (ngine to ren#er the View )esult 0t is han#le# !y IViewEngine interface of the view
engine +y #efault Asp'et MVC uses eb!orman# Ra"or view engines @ou can also register
your own custom view engine to your Asp'et MVC application as shown !elow7
1. protected void 7pplication=5tart()
2. {
3. //Remove All View Engine including Webform and Razor
$. >ie*;ngines.;ngines.Clear()#
(. //Register our !ustom View Engine
). >ie*;ngines.;ngines.7dd(ne* Custo->ie*;ngine())#
1. //"ther code is removed for clarit#
2. }
A View
Action metho# may returns a te5t string/a !inary *le or a >son formatte# #ata The most
important Action )esult is the View)esult/ which ren#ers an# returns an 1TML page to the
!rowser !y using the current view engine
6) )a9or View (ngine V% .e! "orm<A%&B) View (ngine
)a9or View (ngine .e! "orm View (ngine
)a9or (ngine is an a#vance# view
engine that was intro#uce# with
MVC8 This is not a new language
!ut it is a new markup synta5
.e! "orm (ngine is the #efault
view engine for the Aspnet MVC
that is inclu#e# with Aspnet MVC
from the !eginning
The namespace for )a9or (ngine
is %ystem.e!)a9or
The namespace for .e!form
(ngine
is%ystem.e!Mvc.e!"ormView(
ngine
The *le e5tensions use# with
)a9or (ngine are #iCerent from
.e! "orm (ngine 0t has cshtml
<)a9or with CD) or v!html <)a9or
with V+) e5tension for views/
partial views/ e#itor templates an#
for layout pages
The *le e5tensions use# with .e!
"orm (ngine are also like Aspnet
.e! "orms 0t has asp5 e5tension
for views/ asc5 e5tension for
partial views E e#itor templates
an# master e5tension for
layoutFmaster pages
)a9or has new an# a#vance
synta5 that are compact/
e5pressive an# re#uces typing
.e! "orm (ngine has the same
synta5 like Aspnet .e! "orms
uses for asp5 pages
)a9or synta5 are easy to learn an#
much clean than .e! "orm
synta5 )a9or uses G sym!ol to
make the co#e like as7
1.
@Html.ActionLink("SignU
p", "SignUp")
.e! "orm synta5 are !orrowe#
from Aspnet .e! "orms synta5
that are mi5e# with html an#
sometimes make a view messy
.e!form uses HI an# IJ
#elimiters to make the co#e like
as7
1. <%:
Html.ActionLink("SignUp"
, "SignUp") %>
+y #efault/ )a9or (ngine prevents
B%% attacks<CrossK%ite %cripting
Attacks) means it enco#es the
script or html tags like H/J !efore
ren#ering to view
.e! "orm (ngine #oes not prevent
B%% attacks means any script
save# in the #ata!ase will !e *re#
while ren#ering the page
)a9or (ngine is little !it slow as
compare# to .e!form (ngine
.e! "orm (ngine is faster than
)a9or (ngine
)a9or (ngine/ #oesn;t support
#esign mo#e in visual stu#io
means you cannot see your page
look an# feel
.e! "orm engine support #esign
mo#e in visual stu#io means you
can see your page look an# feel
without running the application
)a9or (ngine support T$$ <Test
$riven $evelopment) since it is
not #epen# on
%ystem.e!-0&age class
.e! "orm (ngine #oesn;t support
T$$ <Test $riven $evelopment)
since it #epen#
on %ystem.e!-0&ageclass which
makes the testing comple5

Anda mungkin juga menyukai