MVC Interview questions and answers

download MVC Interview questions and answers

of 45

Transcript of MVC Interview questions and answers

  • 8/9/2019 MVC Interview questions and answers

    1/45

    100 important MVC Interview Question

    By

    Shivprasad Koirala

    AndSukesh Marla

    Do not miss our 2 days ( Saturday and Sunday) full hands on MVC training ourse in Mum!aiall "22#$$%&2'%

    ant to learn MVC online start from the !elo* youtu!e video

    https+,,***-youtu!e-om,*ath.v/0p%nS1m&vk

    Contents

    What is MVC?

    MVC is an arhitetural pattern *hih separates the representation and the user interation- 1t3s divided into three !roader setions4 5Model64 5Vie*6 and 5Controller6- Belo* is ho* eah one of them handles thetask-

    7he 5Vie*6 is responsi!le for look and feel-

    5Model6 represents the real *orld o!8et and provides data to the 5Vie*6-

    7he 5Controller6 is responsi!le to take the end user re9uest and load the appropriate 5Model6 and5Vie*6-

    Figure 6.1:- MVC (Model view controller)

    https://www.youtube.com/watch?v=Lp7nSImO5vkhttps://www.youtube.com/watch?v=Lp7nSImO5vk
  • 8/9/2019 MVC Interview questions and answers

    2/45

    Explain MVC application life cycle?

    Note: - There is nothing as such called as MVC life cycle. I think lot of

    people are obsessed with A!.N"T page life cycle and they think there is life

    cycle in MVC as well. To be specific the MVC re#uest goes through $arioussteps of e%ecution and that&s what is ter'ed as MVC application life cycle.

    Any *e! appliation has t*o main e:eution steps first understanding the re9uest and depending on thetype of the re9uest sending out appropriate response- MVC appliation life yle is not different it has t*omain phases first reating the re9uest o!8et and seond sending our response to the !ro*ser-

    Creating the request object: - 7he re9uest o!8et reation has four ma8or steps- Belo* is the detaile:planation of the same-

    te! 1 Fill route+ # MVC re9uests are mapped to route ta!les *hih in turn speify *hih ontroller andation to !e invoked- So if the re9uest is the first re9uest the first thing is to fill the route ta!le *ith routesolletion- 7his filling of route ta!le happens in the glo!al-asa: file-

    te! " Fetch route:# Depending on the ;

  • 8/9/2019 MVC Interview questions and answers

    3/45

    te! ' %ecute ction: # 7he 5ControllerAtion1nvoker6 determines *hih ation to e:euted ande:eutes the ation-

    te! 6 $esult sent+ # 7he ation method e:eutes and reates the type of result *hih an !e a vie* result4 file result 4 ?S@ result et-

    So in all there are si: !road steps *hih get e:euted in MVC appliation life yle-

    Note :- In case you are not able to re'e'ber the abo$e steps during inter$iew

    re'e'ber the acrony' (()C")*(ight (or )espect Can "$oke )e$olution+.

    Is MVC suitable for both winows an web application?

    MVC arhiteture is suited for *e! appliation than *indo*s- or *indo* appliation MV i-e- 5Modelvie* presenter6 is more applia!le-1fyouareusingandS0MVVMismoresuita!ledueto!indings-

    What are the benefits of usin! MVC?

    7here are t*o !ig !enefits of MVC+#

    Separation of onerns is ahieved as *e are moving the ode !ehind to a separate lass file- Bymoving the !inding ode to a separate lass file *e an reuse the ode to a great e:tent-

    Automated ;1 testing is possi!le !eause no* the !ehind ode (;1 interation ode) has moved toa simple-@>7 lass- 7his gives us opportunity to *rite unit tests and automate manual testing-

    Is MVC ifferent from a " layere architecture?

    MVC is an evolution of a layered traditional arhiteture- Many omponents of layered arhitetureare part of MVC- So !elo* is ho* the mapping goes-

    Functionalit* # la*ered + tiered architecture Model view controller architecture

    0ook and eel ;ser interfae- Vie*-

    ;1 logi ;ser interfae- Controller

    Business logi ,validations Middle layer Model-

  • 8/9/2019 MVC Interview questions and answers

    4/45

    Figure 6.":- # la*ered architecture

    What is the latest version of MVC?

    hen this note *as *ritten4 four versions *here released of MVC- MVC 4 MVC 24 MVC and MVC- So the latest is MVC &-

    What is the ifference between each version of MVC?

    Belo* is a detail ta!le of differenes- But during intervie* it3s diffiult to talk a!out all of them due totime limitation- So 1 have highlighted important differenes *hih you an run through !efore theintervie*er-

    MVC 2 MVC 3 MVC 4

    Client-Side Validation

    Templated HelpersAreas

    Asynchronous Controllers

    Html.ValidationSummary Helper Method

    DefaultValueAttribute in Action-Method

    Parameters

    Binding Binary Data with Model Binders

    DataAnnotations Attributes

    Razor

    eadymade pro!ecttemplates

    HTML ena!led templates

    Support "or Multiple Vie#

    $n%ines

    &a'aScript and A(a)

    Model Validation

    "mpro#ements

    AS*+,$T e! A*.

    efreshed and moderni$ed defaultpro!ect templates

    %ew mobile pro!ect template

    Many ne# "eatures to support

    mo!ile apps

    &nhanced support for

    asynchronous methods

  • 8/9/2019 MVC Interview questions and answers

    5/45

    Model-Validator Pro#iders

    %ew e'uireHttpsAttribute Action (ilter

    )emplated Helpers

    Display Model-*e#el &rrors

    What are #$M% helpers in MVC?

    =7M0 helpers help you to render =7M0 ontrols in the vie*- or instane if you *ant to display a=7M0 te:t!o: on the vie*4 !elo* is the =7M0 helper ode-

    or hek!o: !elo* is the =7M0 helper ode- 1n this *ay *e have =7M0 helper methods for every=7M0 ontrol that e:ists-

    What is the ifference between $M%'$ext(ox) vs $M%'$ext(ox*or)?

    Both of them provide the same =7M0 output4 5=7M0-7e:tBo:or6 is strongly typed *hile

    5=7M0-7e:tBo:6 isn3t-

    Belo* is a simple =7M0 ode *hih 8ust reates a simple te:t!o: *ith 5CustomerCode6 as name-

    Html.TextBox("CustomerCode")

    Belo* is 5=tml-7e:tBo:or6 ode *hih reates =7M0 te:t!o: using the property nameECustomerCode6 from o!8et 5m6-

    Html.TextBoxor(m => m.CustomerCode)

    1n the same *ay *e have for other =7M0 ontrols like for hek!o: *e have 5=tml-ChekBo:6 and5=tml-ChekBo:or6-

  • 8/9/2019 MVC Interview questions and answers

    6/45

    Explain the importance of MVC moel biners?

    Model !inder maps =7M0 form elements to the model- 1t ats like a !ridge !et*een =7M0 ;1 and MVCmodel-

    7ake the !elo* simple =7M0 form e:ample+#

    Customer code :-

    Customer name :-

    @o* this form needs to fill the !elo* 5Customer6 lass model- 1f you see the =7M0 ontrol name theyare different from the lass property name- or e:ample =7M0 te:t!o: ontrol name is 5CCode6 and thelass property name is 5CustomerCode6- 7his mapping ode is *ritten in =7M0 !inder lasses-

    pubic cass Customer

    !

    pubic strin CustomerCode ! et# set# $

    pubic strin CustomerName ! et# set# $

    $

    7o reate a model !inder *e need to implement 51ModelBinder6 interfae and mapping ode needs to !e*ritten in the 5BindModel6 method as sho*n in the !elo* ode-

    pubic cass Customer%inder : &'ode%inder

    !

    pubic ob(ect %ind'ode)ControerContext controerContext* 'ode%indinContext

    bindinContext+

  • 8/9/2019 MVC Interview questions and answers

    7/45

    !

    ,ttpe.uest%ase re.uest = controerContext,ttpContexte.uest#

    strin strCustomerCode = re.uest0ormet)"CCode"+#

    strin strCustomerName = re.uest0ormet)"CName"+#

    return ne2 Customer

    !

    CustomerCode = strCustomerCode*

    CustomerName = strCustomerName

    $#

    $

    $

    @o* in the ation result method *e need to use the 5ModelBinder6 attri!ute *hih *ill attah the !inder*ith the lass model-

    pubic 3ctionesut SubmitCustomer)4'ode%inder)typeof)Customer%inder++5Customer ob(+

    !

    return 6ie2)78ispayCustomer+#

    $

    What are routin! in MVC?

  • 8/9/2019 MVC Interview questions and answers

    8/45

    Where is the route mappin! coe written?

    7he route mapping ode is *ritten in the 5glo!al-asa:6 file-

    Can we map multiple +,%-s to the same action?

    Fes 4 you an 4 you 8ust need to make t*o entries *ith different key names and speify the same ontrollerand ation-

    #ow can we navi!ate from one view to other view usin! hyperlin.?

    By using 5Ation0ink6 method as sho*n in the !elo* ode- 7he !elo* ode *ill reate a simple ;

  • 8/9/2019 MVC Interview questions and answers

    9/45

    Figure 6.#:- di,,erence between te!data viewdata and viewbag

    /e! data: -=elps to maintain data *hen you move from one ontroller to other ontroller or from oneation to other ation- 1n other *ords *hen you rediret45tempdata6 helps to maintain data !et*een thoseredirets- 1t internally uses session varia!les-

    View data: - =elps to maintain data *hen you move from ontroller to vie*-

    View 0ag: -1t3s a dynami *rapper around vie* data- hen you use 5Vie*!ag6 type asting is not

    re9uired- 1t uses the dynami key*ord internally-

    Figure 6.&:-d*naic e*word

  • 8/9/2019 MVC Interview questions and answers

    10/45

    ession variables+ # By using session varia!les *e an maintain data from any entity to any entity-

    2idden ,ields and 2/M3 controls+ # =elps to maintain data from ;1 to ontroller only- So you an senddata from =7M0 ontrols or hidden fields to the ontroller using S7 or G>7 =77 methods-

    Belo* is a summary ta!le *hih sho*s different mehanism of persistene-

    Maintains data between View4ata+View0ag /e!4ata 2idden ,ields ession

    Controller to Controller @o Fes @o Fes

    Controller to View Fes @o @o Fes

    View to Controller @o @o Fes Fes

    What is life of &$emp4ata) ?

    57empData6 is availa!le for the urrent re9uest and in the su!se9uent re9uest it3s availa!le depending on*hether 57empData6 is read or not-

    So if 57empData6 is one read it *ill not !e availa!le in the su!se9uent re9uest-

    What is the use of 5eep an ee. in &$emp4ata)?

    ne 57empData6 is read in the urrent re9uest it3s not availa!le in the su!se9uent re9uest- 1f *e *ant57empData6 to !e read and also availa!le in the su!se9uent re9uest then after reading *e need to all5Keep6 method as sho*n in the ode !elo*-

    7Tem!4ata18M54ata92-

    Tem!4ata.:ee!(8M54ata9)-

    7he more shortut *ay of ahieving the same is !y using 5eek6- 7his funtion helps to read as *elladvies MVC to maintain 57empData6 for the su!se9uent re9uest-

    stri'; str = Tem!4ata.+eek("Td").Totri';()-

    What are partial views in MVC?

    artial vie* is a reusa!le vie* (like a user ontrol) *hih an !e em!edded inside other vie*-or e:ample let3s say all your pages of your site have a standard struture *ith left menu4 headerand footer as sho*n in the image !elo*-

  • 8/9/2019 MVC Interview questions and answers

    11/45

    Figure 6.':- !artial views in MVC

    or every page you *ould like to reuse the left menu4 header and footer ontrols- So you an goand reate partial vie*s for eah of these items and then you all that partial vie* in the mainvie*-

    #ow i you create partial view an consume the same?

    hen you add a vie* to your pro8et you need to hek the 5Create partial vie*6 hek !o:-

  • 8/9/2019 MVC Interview questions and answers

    12/45

    Figure6.6:-create!artialview

    ne the partial vie* is reated you an then all the partial vie* in the main vie* using5=tml-

  • 8/9/2019 MVC Interview questions and answers

    13/45

    !u3lic stri'; CustomerCode

    set-

    ;et-

    1n order to display the validation error message *e need to use 5ValidateMessageor6 method *hih!elongs to the 5=tml6 helper lass-

    m.CustomerCode)%>

    0ater in the ontroller *e an hek if the model is proper or not !y using 5ModelState-1sValid6 propertyand aordingly *e an take ations-

    !u3lic /ctio'esult +ostCustomer(Customer o36)

    i (Modeltate.@s#alid)

    o36.ae()-

    retur' #ie$("Tha'ks")-

    else

    retur' #ie$("Customer")-

    Belo* is a simple vie* of ho* the error message is displayed on the vie*-

  • 8/9/2019 MVC Interview questions and answers

    14/45

    Figure 6.5:- validations in MVC

    Can we isplay all errors in one !o?

    Fes *e an4 use 5ValidationSummary6 method from =7M0 helper lass-

  • 8/9/2019 MVC Interview questions and answers

    15/45

    1f you have reated the model o!8et yourself you an e:pliitly all 57ry;pdateModel6 in yourontroller to hek if the o!8et is valid or not-

    Tr5*!dateModel(Ne$Customer)-

    1n ase you *ant add errors in the ontroller you an use 5AddModel>rror6 funtion-

    Modeltate./ddModel?rror("irstName" "This is m5 sererDside error.")-

    #ow can we enable ata annotation valiation on client sie?

    1t3s a t*o#step proess first referene the neessary 89uery files-

    Explain 6reas in MVC?

    Areas help you to group funtionalities in to independent modules thus making your pro8et moreorganiHed- or e:ample in the !elo* MVC pro8et *e have four ontroller lasses and as time passes !yif more ontroller lasses are added it *ill !e diffiult to manage- 1n !igger pro8ets you *ill end up *ith""3s of ontroller lasses making life hell for maintenane-

  • 8/9/2019 MVC Interview questions and answers

    16/45

    1f *e an group ontroller lasses in to logial setion like 51nvoiing6 and 5Aounting6 that *ouldmake life easier and that3s *hat 5Area6 are meant to-

    Fou an add an area !y right liking on the MVC solution and liking on 5Area6 menu as sho*n in the

    !elo* figure-

    1n the !elo* image *e have t*o 5Areas6 reated 5Aount6 and 51nvoiing6 and in that 1 have put therespetive ontrollers- Fou an see ho* the pro8et is looking more organiHed as ompared to the previousstate-

  • 8/9/2019 MVC Interview questions and answers

    17/45

    What is ra7or in MVC?

    1t3s a light *eight vie* engine- 7ill MVC *e had only one vie* type i-e-ASI4

  • 8/9/2019 MVC Interview questions and answers

    18/45

    Explain the ifference between layout an master pa!es ?

    0ayout are like master pages in AS-@>7 e! form- Master pages give a standard look and feel for e!form vie*s *hile layout gives standard look and feel or ats like a template for raHor vie*s-

    #ow to apply layout to ,a7or views?

    So first *e need to reate a template file as sho*n in the !elo* ode-

    7e'derectio'("Header")

    7e'derBod5()

    7e'derectio'("ooter")

    And then apply this template to the vie* as sho*n !elo* and display data in those respetive setions-

    7La5out = "ie$s&4eaultA&La5out+a;eA.cshtml"-

    This is 3od5

    7sectio' ooterCo!5ri;ht KADKA

    7sectio' HeaderOelcome to m5 site

    Explain the concept of 2caffolin!?

    Note PD 4o 'ot ;et scared $ith the $ord. @ts actuall5 a er5 sim!le thi';.

    Saffolding is a tehni9ue in *hih the MVC template helps to auto#generate C

  • 8/9/2019 MVC Interview questions and answers

    19/45

    1t reates ontroller ode4 vie* and also ta!le struture as sho*n in the !elo* figure-

  • 8/9/2019 MVC Interview questions and answers

    20/45

    What oes scaffolin! use internally to connect to atabase?

    1t uses >ntity frame*ork internally-

  • 8/9/2019 MVC Interview questions and answers

    21/45

    #ow can you o authentication an authori7ation in MVC?

    Fou an use *indo*s or forms authentiation for MVC-

    #ow to implement winows authentication for MVC?

    or *indo*s authentiation you need to go and modify the 5*e!-onfig6 file and set authentiation modeto *indo*s-

    7- So the first step is to setauthentiation mode e9ual to forms- 7he 5login;rl6 points to a ontroller here rather than page-

  • 8/9/2019 MVC Interview questions and answers

    22/45

    e also need to reate a ontroller *here *e *ill hek the user is proper or not- 1f the user is proper *e*ill set the ookie value-

    !u3lic /ctio'esult Lo;i'()

    i ((euest.orm1"txt*serName"2 == "hi") UU (euest.orm1"txt+ass$ord"2

    == "hi7AKR"))

    orms/uthe'ticatio'.et/uthCookie("hi"true)-

    retur' #ie$("/3out")-

    else

    retur' #ie$("@'dex")-

    All the other ations need to !e attri!uted *ith 5AuthoriHe6 attri!ute so that any unauthoriHed user if hemakes a all to these ontrollers it *ill rediret to the ontroller ( in this ase the ontroller is 50ogin6)*hih *ill do authentiation-

    1/uthoriFe2

    +u3lic/ctio'esult 4eault()

    retur' #ie$()-

    1/uthoriFe2

    !u3lic/ctio'esult /3out()

    retur' #ie$()-

    #ow to implement 69ax in MVC?

    Fou an implement A8a: in t*o *ays in MVC+#

  • 8/9/2019 MVC Interview questions and answers

    23/45

    A8a: li!raries

    ?9uery

    Belo* is a simple sample of ho* to implement A8a: !y using 5A8a:6 helper li!rary- 1n the !elo*ode you an see *e have a simple form *hih is reated !y using 5A8a:-Beginorm6 synta:-

    7his form alls a ontroller ation alled as 5getCustomer6- So no* the su!mit ation lik *ill!e an asynhronous a8a: all-

    1script language6a$ascript5

    function 7nuccess*data8+

    9

    ,, 0o so'ething here

    1,script5

    1di$512

    $ar A6a%7pt new A6a%7ptions97nuccess7nuccess4

    25

    12 using *A6a%.;egin(or'*getCusto'er/MyA6a%/A6a%7pt++ 9 25

    1input idt%tCusto'erCode typete%t ,51br ,5

    1input idt%tCusto'erNa'e typete%t ,51br ,5

    1input idub'it

  • 8/9/2019 MVC Interview questions and answers

    24/45

    funtion on the ontroller 4 !elo* is the ode for the same- ne the ontroller responds this data isdisplayed in the =7M0 D1V tag !y name 5DateDiv6-

    1span id0ate0i$ ,5

    12:

    A6a%.Action=ink*>et 0ate/>et0ate/

    new A6a%7ptions 9?pdateTargetId 0ate0i$ +

    25

    Belo* is the ontroller ode- Fou an see ho* 5GetDate6 funtion has a pause of " seonds-

    public class 0efault8Controller : Controller

    9

    public string >et0ate*+

    9

    Thread.leep*8@@@@+4

    return 0ateTi'e.Now.Totring*+4

    7he seond *ay of making A8a: all in MVC is !y using ?9uery- 1n the !elo* ode you an see *e aremaking an a8a: S7 all to a ;et0ata*+

    9

    $ar url ,MyA6a%,getCusto'er4

    .post*url/ function *data+

    9

    *Bt%tCusto'erCode+.$al*data.Custo'erCode+4

    *Bt%tCusto'erNa'e+.$al*data.Custo'erNa'e+4

    +

  • 8/9/2019 MVC Interview questions and answers

    25/45

    What .in of events can be trac.e in 6:68 ?

    Figure 6.7:- traced in 89

    What is the ifference between &6ction,esult) an &View,esult)?

    5Ation

  • 8/9/2019 MVC Interview questions and answers

    26/45

    7here 2 kinds of results in MVC4 at the top is 5Ation

  • 8/9/2019 MVC Interview questions and answers

    27/45

    - 1nspet the returned value-

    - rovide e:tra data to the ation-

    Fou an reate ation filters !y t*o *ays+#

    1nline ation filter-

    Creating an 5Ationilter6 attri!ute-

    7o reate a inline ation attri!ute *e need to implement 51Ationilter6 interfae-7he 51Ationilter6interfae has t*o methods 5nAtion>:euted6 and 5nAtion>:euting6- e an implement pre#proessing logi or anellation logi in these methods-

    public class 0efault8Controller : Controller / IAction(ilter

    9

    public Action)esult Inde%*Custo'er ob6+

    9

    return View*ob6+4

    $oid IAction(ilter.7nAction"%ecuted*Action"%ecutedConte%t

    filterConte%t+

    9

    Trace.Frite=ine*Action "%ecuted+4

    $oid IAction(ilter.7nAction"%ecuting*Action"%ecutingConte%t

    filterConte%t+

    9 Trace.Frite=ine*Action is e%ecuting+4

    7he pro!lem *ith inline ation attri!ute is that it annot !e reused aross ontrollers- So *e an onvertthe inline ation filter to an ation filter attri!ute- 7o reate an ation filter attri!ute *e need to inheritfrom 5AtionilterAttri!ute6 and implement 51Ationilter6 interfae as sho*n in the !elo* ode-

    public class MyActionAttribute : Action(ilterAttribute / IAction(ilter

    9

    $oid IAction(ilter.7nAction"%ecuted*Action"%ecutedConte%t filterConte%t+

    9

    Trace.Frite=ine*Action "%ecuted+4

  • 8/9/2019 MVC Interview questions and answers

    28/45

    $oid IAction(ilter.7nAction"%ecuting*Action"%ecutingConte%t filterConte%t+

    9

    Trace.Frite=ine*Action e%ecuting+4

    0ater *e an deorate the ontrollers on *hih *e *ant the ation attri!ute to e:eute- Fou an see in the!elo* ode 1 have deorated the 5DefaultController6 *ith 5MyAtionAttri!ute6 lass *hih *as reatedin the previous ode-

    GMyActionAttributeH

    public class 0efault8Controller : Controller

    9

    public Action)esult Inde%*Custo'er ob6+

    9

    return View*ob6+4

    Can we create our custom view en!ine usin! MVC?

    Fes4 *e an reate our o*n ustom vie* engine in MVC- 7o reate our o*n ustom vie* engine *e need

    to follo* steps+#0et3 say *e *ant to reate a ustom vie* engine *here in the user an type a ommand like5LDate7ime6 and it should display the urrent date and time-

    te! 1+# e need to reate a lass *hih implements 51Vie*6 interfae- 1n this lass *e should *rite thelogi of ho* the vie* *ill !e rendered in the 5render6 funtion- Belo* is a simple ode snippet for thesame-

    public class MyCusto'View : IView

    9

    pri$ate string (older!ath4 ,, 0efine where our $iews are stored

    public string (older!ath

    9

    get 9 return (older!ath4

    set 9 (older!ath $alue4

  • 8/9/2019 MVC Interview questions and answers

    29/45

    public $oid )ender*ViewConte%t $iewConte%t/ yste'.I7.Te%tFriter

    writer+ 9

    ,, !arsing logic 1dateTi'e5

    ,, read the $iew file

    string str(ile0ata (ile.)eadAllTe%t*(older!ath+4

    ,, we need to and replace 1dateti'e5 dateti'e.now $alue

    string str(inal str(ile0ata.)eplace*10ateTi'e5/

    0ateTi'e.Now.Totring*++4

    ,, this replaced data has to sent for display

    writer.Frite*str(inal+4

    te! " :-e need to reate a lass *hih inherits from 5VirtualathroviderVie*>ngine6 and inthis lass *e need to provide the folder path and the e:tension of the vie* name- or instane forraHor the e:tension is 5shtml6 4 for asp: the vie* e:tension is 5-asp:6 4 so in the same *ay forour ustom vie* *e need to provide an e:tension- Belo* is ho* the ode looks like- Fou an see

    the 5Vie*0oationormats6 is set to the 5Vie*s6 folder and the e:tension is 5-myvie*6-

    public class MyView"ngine!ro$ider : Virtual!ath!ro$iderView"ngine

    9

    ,, Fe will create the ob6ect of Mycusto'e $iew

    public MyView"ngine!ro$ider*+ ,, constructor

    9

    ,, 0efine the location of the View file

    this.View=ocation(or'ats new stringGH 9 J,Views,98,

    9@.'y$iew/ J,Views,hared,9@.'y$iew 4 ,,location and e%tension of our$iews

    protected o$erride IView CreateView*ControllerConte%t

    controllerConte%t/ string $iew!ath/ string 'aster!ath+

    9

  • 8/9/2019 MVC Interview questions and answers

    30/45

    $ar physicalpath

    controllerConte%t.3ttpConte%t.er$er.Map!ath*$iew!ath+4

    MyCusto'View ob6 new MyCusto'View*+4 ,, Custo' $iew engine

    class

    ob6.(older!ath physicalpath4 ,, set the path where the $iewswill be stored

    return ob64 ,, returned this $iew paresing logic so that it can

    be registered in the $iew engine collection

    protected o$erride IView Create!artialView*ControllerConte%t

    controllerConte%t/ string partial!ath+

    9

    $ar physicalpath

    controllerConte%t.3ttpConte%t.er$er.Map!ath*partial!ath+4

    MyCusto'View ob6 new MyCusto'View*+4 ,, Custo' $iew engine

    class

    ob6.(older!ath physicalpath4 ,, set the path where the $iews

    will be stored

    return ob64 ,, returned this $iew paresing logic so that it can

    be registered in the $iew engine collection

    te! #:-e need to register the vie* in the ustom vie* olletion- 7he !est plae to register the ustomvie* engine in the 5Vie*>ngines6 olletion is the 5glo!al-asa:6 file- Belo* is the ode snippet for thesame-

    protected $oid Applicationtart*+

    9

    ,, tepK :- register this ob6ect in the $iew engine collection

    View"ngines."ngines.Add*new MyView"ngine!ro$ider*++4

    L..

    Belo* is a simple output of the ustom vie* *ritten using the ommands defined at the top-

  • 8/9/2019 MVC Interview questions and answers

    31/45

    Figure6.11:-custoviewengineusingMVC

    1f you invoke this vie* you should see the follo*ing output-

    Figure 6.1":- out!ut

    #ow to sen result bac. in :2; format in MVC?

    1n MVC *e have 5?sonet+4

    Belo* is the ?S@ output of the a!ove ode if you invoke the ation via the !ro*ser-

    Figure 6.1#:- 8=> ,orat in MVC

  • 8/9/2019 MVC Interview questions and answers

    32/45

    What is &Web6I)?

    =77 is the most used protool-or past many years !ro*ser *as the most preferred lient !y *hih *e

    an onsume data e:posed over =77- But as years passed !y lient variety started spreading out- e haddemand to onsume data on =77 from lients like mo!ile48avasripts4*indo*s appliation et-

    or satisfying the !road range of lient 5S76 *as the proposed approah- Fou an read more a!out5S76 from C hapter-

    5e!A16 is the tehnology !y *hih you an e:pose data over =77 follo*ing S7 priniples-

    #ow oes WC* iffer from WE( 6I ?

    ?CF ?0 @A

    Multi-!rotocol hosting =eavy *eight !eause of ompliatedSD0 struture-

    0ight *eight4 only the neessaryinformation is transferred-

    @rotocol 1ndependent of protools- nly for =77 protool

    Forats 7o parse SA message4 the lientneeds to understand SD0 format-riting ustom ode for parsing SD0is a heavy duty task- 1f your lient issmart enough to reate pro:y o!8etslike ho* *e have in -@>7 (addreferene) then SA is easier toonsume and all-

    utput of 5e!A16 are simple stringmessage4?S@4Simple IM0 format et- So*riting parsing logi for the same in veryeasy-

    @rinci!les SA follo*s S#N speifiation- >B A1 follo*s S7 priniples- (leaserefer a!out S7 in C hapter)-

    With WC* also you can implement ,E2$32o why

  • 8/9/2019 MVC Interview questions and answers

    33/45

    Figure 6.1&:- i!leent ;?eb@A< in MVC

    te! "+# ne you have reated the pro8et you *ill notie that the ontroller no* inherits from

    OApiControllerO and you an no* implement OpostO4OgetO4OputO and OdeleteO methods of =77 protool-

    public class ValuesController : ApiController

    9

    ,, >"T api,$alues

    public I"nu'erable1string5 >et*+

    9

    return new stringGH 9 $alue8/ $alue"T api,$alues,

    public string >et*int id+

    9

    return $alue4

    ,, !7T api,$alues

    public $oid !ost*G(ro';odyHstring $alue+

    9

    ,, !?T api,$alues,

    public $oid !ut*int id/ G(ro';odyHstring $alue+

    9

  • 8/9/2019 MVC Interview questions and answers

    34/45

    ,, 0"="T" api,$alues,

    public $oid 0elete*int id+

    9

    te! #:-1f you make a =77 G>7 all you should get the !elo* results-

    Figure 6.1':- 2//@

    #ow can we etect that a MVC controller is calle by 2$ or /E$ ?

    7o detet if the all on the ontroller is 5S76 ation or a 5G>76 ation *e an use5

  • 8/9/2019 MVC Interview questions and answers

    35/45

  • 8/9/2019 MVC Interview questions and answers

    36/45

    2o how o we implement bunlin! in MVC ?

    pen the 5BundleConfig-s6 from the 5AppQStart6 folder-

    1n the 5BundleConfig-s6 add the ?S files *hih you *ant !undle in to single entity in to the!undles olletion- 1n the !elo* ode *e are om!ining all the 8avasript ?S files *hih aree:isting in the 5Sripts6 folder as a single unit in to the !undle olletion-

    bundles.Add*new cript;undle*J,cripts,Mycripts+.Include*

    J,cripts,.6s++4

    Belo* is ho* your 5BundleConfig-s6 file *ill look like-

    public class ;undleConfig9

    public static $oid )egister;undles*;undleCollection bundles+

    9

    bundles.Add*new cript;undle*J,cripts,Mycripts+.Include*

  • 8/9/2019 MVC Interview questions and answers

    37/45

    J,cripts,.6s++4

    ;undleTable."nable7pti'iOations true4

    ne you have om!ined your sripts in to one single unit *e then to inlude all the ?S files in tothe vie* using the !elo* ode- 7he !elo* ode needs to put in the ASI or na!leptimiHations6 to true in the5!undleonfig-s6 file or else you *ill not see the !undling effet in the page re9uests-

    ;undleTable."nable7pti'iOations true4

    Explain minification an how to implement the same ?

    Minifiation redues the siHe of sript and CSS files !y removing !lank spaes 4 omments et-or e:ample !elo* is a simple 8avasript ode *ith omments-

    ,, This is test

    $ar % @4

    % % P 84

    % %

  • 8/9/2019 MVC Interview questions and answers

    38/45

    After implementing minifiation the 8avasript ode looks something as !elo*- Fou an see ho**hitespaes and omments are removed to minimiHe file siHe and thus inreasing performane-

    $ar %@4%%P84%%

  • 8/9/2019 MVC Interview questions and answers

    39/45

    Customer@ame 7:tCustomer@ame

    Amount 7:tAmount

    CustomerBuying0evelColor

    #ow can we use two = multiple> moels with a sin!le view?

    0et us first try to understand *hat the intervie*er is asking- hen *e !ind a model *ith a vie**e use the model dropdo*n as sho*n in the !elo* figure- 1n the !elo* figure *e an only seletone model-

    But *hat if *e *ant to !ind 5Customer6 as *ell as 5rder6 lass to the vie*-

    or that *e need to reate a vie* model *hih aggregates !oth the lasses as sho*n in the !elo*ode- And then !ind that vie* model *ith the vie*-

    !u3lic class Cust,rder#M

    !u3lic Customer cust = 'e$ Customer()-

    !u3lic ,rder ,rd = 'e$ ,rder()-

    1n the vie* *e an refer !oth the model using the vie* model as sho*n in the !elo* ode-

  • 8/9/2019 MVC Interview questions and answers

    40/45

    What .in of lo!ic view moel class will have ?

    As the name says vie* model this lass has the gel ode or onnetion ode *hih onnets thevie* and the model-

    So the vie* model lass an have follo*ing kind of logis+#

    Color trans,oration logic+ # or e:ample you have a 5Grade6 property in model and

    you *ould like your ;1 to display 5red6 olor for high level grade4 5yello*6 olor for

    lo* level grade and 5green6 olor of ok grade-

    4ata ,orat trans,oration logic +# Four model has a property 5Status6 *ith 5Married6

    and 5;nmarried6 value- 1n the ;1 you *ould like to display it as a hek!o: *hih is

    heked if 5married6 and unheked if 5unmarried6-

    ggregation logic: - Fou have t*o different Customer and Address model lasses and

    you have vie* *hih displays !oth 5Customer6 and 5Address6 data on one go-

    tructure downsiBing: -Fou have 5Customer6 model *ith 5ustomerCode6 and

    5Customer@ame6 and you *ant to display 8ust 5Customer@ame6- So you an reate a

    *rapper around model and e:pose the neessary properties-

    What is the use of &6llow#$M%) an &ValiateInput) attributes?

    hile *orking *ith MVC you may have notied that MVC ations doesn3t allo* posting =7M0values - 7his is done to avoid ross site sripting seurity issues-0ook at the follo*ing e:ample*here *e are trying to post =7M0 to the MVC ation-

  • 8/9/2019 MVC Interview questions and answers

    41/45

    But you an !ypass this restrition !y using one of the follo*ing 2 attri!utes

    ) Validate1nput attri!ute at ontroller level or ation level

    2) Allo*=tml attri!ute at Mode 0evel

    Explain unobtrusive :ava2cript?

    ;no!trusive ?avaSript helps you to deouple presentation and !ehavior- Consider the !elo*!utton ode it has t*o parts one is the ;1 i-e- the !utton itself and seond is the !ehavior i-e-5Sho*Alert()6-

  • 8/9/2019 MVC Interview questions and answers

    42/45

    1n other *ords the !utton is tied up *ith the 5Sho*Alert6 ation- 1t *ould !e great if *e oulddeouple the !ehavior from the !utton so that any other !ehavior an !e attahed *ith the !utton-

    1input typebutton onclickhowAlert*+ idbtn ,5

    1script5

    function howAlert*+

    9

    alert*3ello+4

    1,script5

    0ook at this !utton ode you an see no ation is attahed *ith the !elo* !utton element-

    1input typebutton idbtn ,5

    Ation to the !utton is attahed on runtime as sho*n in the !elo* ode- 1f needed tomorro* *ean attah some other !ehavior *ith 5!tn6 !utton - So uno!trusive 8avasript is nothing !utdeoupling the presentation from the !ehavior-

    1script5

    function howAlert*+

    9

    alert*3ello+4

    $ar el docu'ent.get"le'ent;yId*btn+4

    el.onclick howAlert4

    1,script5

    Explain the nee of isplay moe in MVC?

    Display mode displays vie*s depending on the devie the user has logged in *ith- So *e anreate different vie*s for different devies and display mode *ill handle the rest-

    or e:ample *e an reate a vie* 5=ome-asp:6 *hih *ill render for the desktop omputersand=ome-Mo!ile-asp:for mo!ile devies- @o* *hen an end user sends a re9uest to the MVCappliation4 display mode heks the 5user agent6 headers and renders the appropriate vie* tothe devie aordingly-

    http://www.codeproject.com/Articles/556995/Home.Mobile.aspxhttp://www.codeproject.com/Articles/556995/Home.Mobile.aspx
  • 8/9/2019 MVC Interview questions and answers

    43/45

    #ow can we valiate usin! faceboo. or twitter accounts =MVC 6uth> ?

    ne of the most !oring proess for an end user is registering on a site- Sometimes those longforms and email validation 8ust puts off the user- So ho* a!out making things easy !y validatingthe users using their e:isting fae!ook , t*itter , linkedin , et aounts- So the user usessomething *hih he already has *hile the site is assured that this user is a proper user-

    7his is ahieved !y using MVC Auth (pen standard for authoriHation)-

    ;sing MVC auth is a three step proess+#

  • 8/9/2019 MVC Interview questions and answers

    44/45

    @e:t step is to open 5AuthConfig-s6 and you *ill see lot of readymade ode to user your1D and Key- So use the appropriate method as per site and provide the 1D and Key-

    7AuthFebecurity.)egisterMicrosoftClient*

    clientId: /

    clientecret: +4

    7AuthFebecurity.)egisterTwitterClient*

    consu'erQey: /

    consu'erecret: +4

    7AuthFebecurity.)egister(acebookClient*

    appId: /

    appecret: +4

    7AuthFebecurity.)egister>oogleClient*+4

    @o* if you run your appliation you should get a link to B login as sho*n in the !elo*sreen- So if you login in B it *ill rediret to your MVC appliation-

  • 8/9/2019 MVC Interview questions and answers

    45/45

    What is new in MVC ?

    Belo* are the ne* features in MVC & +#

    ne AS-@>7

    Attri!ute !ased routing

    Asp-@et 1dentity

    Bootstrap in the MVC template

    Authentiation ilters

    ilter overrides

    @e* Saffolding system