Thursday 27 November 2014

MVC Interview Question

What is MVC ?
MVC stands for Model View Controller, It is a framework pattern or software design which splits the application into three component. They are :
  1.    Model  (Data Access Layer)
  2.       View  ( Presentation Layer)
  3.       Controller (Business Logic )
Model :  Model is a business  entity on which overall  application operates. It represents a set of class which describes the business logic .It also define a business rule, means that how our data can be retrieve, change or manipulate.
View : View represents a User Interface(UI). It is responsible for displaying the data that comes from the controller as a result. We can also say that, It is a user interface that render the model into form. Its basic components are Html, Css, Jquery etc.
Controller: The Controller is responsible to process the data that comes from the user via view. It process the user data with the help of model and passes the result back to the view. Typically, it acts as a co-ordinator between view and Model.


What is the page Lifecycle of the MVC?

MVC page lifecycle can be define as follows :-
 i) App Initialisation 
 In this stage, the aplication starts up by running Global.asax’s Application_Start() method.
 
 In this method, you can add Route objects to the static RouteTable.Routes collection.
 
 If you’re implementing a custom IControllerFactory, you can set this as the active controller  factory by assigning it to the System.Web.Mvc.ControllerFactory.Instance property.
 

 ii) Routing
 
 Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL  pattern.
 
 MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers  configured in the Routes table.
 

 iii) Instantiate and Execute Controller
 
 At this stage, the active IControllerFactory supplies an IController instance.
 

 iv) Locate and invoke controller action
 
 At this stage, the controller invokes its relevant action method, which after further processing,  calls RenderView().
 

 v) Instantiate and render view
 
 At this stage, the IViewFactory supplies an IView, which pushes response data to the  IHttpResponse object.

     What is MVC Architecture.

  MVC Architecture can be easily understand by the following figure and the explanation.



 1.   Browser sends the request to the IIS.
 2.   ISS searches route defined in MVC and passes to controller as per route.
 3.  Controller communicate the model and passes populated model to view.
 4. View are populated with model property via controller.
 5.  Controller passes the response to browser  through IIS.

What is the advantage of an MVC?
Following are an advantage of MVC :-
 1.   MVC provides a clean separation of application concern among  Model(Data Access Layer) View(Presentation Layer)  and Controller(Business Logic ).
2.  It is easy for unit Testing.
3.  It provides improved reusability of View and improved structuring of code.
4.  It gives us the full control over the behavior of application as it does not use Viewstate or server based form.
5.   It provides better support to Test Driven Development.


What is the new features in MVC3 and MVC4 ?
New features of MVC3 :
1.       Razor View Engine.
2.       Support form .Net 4 Data Annotation.
3.       Improved model validation.
4.       Better javascript support,Jquery validation and JSON Binding.

New Features in MVC4 :
1.   Web API
2.   Enhancement to default project template.
3.   Mobile project template using jquery mobile displaying model.
4.  Task support for Asynchronous controller.

What is Razor view Engine?
1.       Razor view is the first major update to render HTML in MVC3, It is the part of new rendering framework.
2.       It is lighter then the Asp.Net render engine. Becouse Asp.Net rendering engine user opening and closing bracket.  Whereas razor view allows clean syntax.
3.       It is easier to type, read and learn.
4.       Razor does not have XML like heavy syntax for web form.  

What is ViewData and ViewBag? Also describe its similarity and Difference.
View Data : In Viewdata objects are accessible using  string key. When we go to use an object on the view then we have to cast it, Because the viewdata  stores everything as object. In otherway, It is a dictionary of object which derives from the viewdatadictionary class and accessible using string as key.
ViewBag :  It is a dynamic properties that takes advantage of new features in c# 4.0. It allows the object to dynamically have the properties add to it.There are no need to cast the object when using data in view. This is because the dynamic we uses let us know the type.

Similarity between ViewData and viewBag.

1. Both maintain the data when moving from controller to view.
2. Both passes data from controller to respective view.
3. The value becomes null when any redirection occers.

Difference between Viewdata and viewbag.

1. ViewData is a dictionary of object that derives from the viewdatadictionary class. And its objects accessible using string as key. Where as viewbag is a dynamic property that takes advantage of new features in C# 4.0.It allows the object to dynamically property added to it.
2. It requires typecasting for complex data type and checks for null value to avoid error. While a viewbag does not requires typecasting for complex datatype.

What is Tempdata in MVC ?
1. Tempdata is a dictionary of object which derived  from the tempdatadictionary class.
2. It help us to maintain the data when we move from one controller to another and one action
        to another action.
3. It keeps the information for the time httprequest only.Means, only from one page to another.
4. When we redirect,  tempdata  helps to maintain data between those redirect.It internally uses
        session veriable.
5. It is a string key and object value.
6. It  requires typecasting for complex data type and checks null value to avoid error.
7. Generally, It is used to store only one time message like error message, validation message etc.

What is Repository pattern in MVC? what are the benefits of it?
Repository pattern basically works as a mediator between our  Business Logic Layer and and Data Access Layer. Sometimes it would be troublesome to expose the data access mechanism to directly to Business Logic Layer, It may result in redundant code for accessing data for similar entity or it may be result in code that is hard to test and understand. To overcome this kind of issue and write interface driven and test driven code to access data we uses repository pattern.
Benefits of repository pattern:
1. It centralize the data logic or the service access logic.
2. It provides substitution point for unit test.
3. It provides a flexible architecture that can be adopted as overall design.

What is Routing in MVC ?
A route is a stand alone component or url pattern that Is mapped to a handler (IHTPPheldler).Handler can be any physical file such as .aspx page or other web form. It is responsible for mapping incoming browser request to MVC controller action.
1. Router matches incoming request from browser to controller action ( IHTPPhandler by using URL pattern).

2. It also construct outgoing URL that correspond to acton.

Segment of default route in MVC
There are three segment of default route. Thery are :
1. Controller name
2. Action Name
3. Parameter                            

  Ex :  http://bccenter.com/Photogalary/GetImage/22
Controller Name = Photogalary
Action Method Name = GetImage
Parameter Id = 22

How route table created in MVC ?
When an MVC application starts, It calls the Application_Start() method from the global.ascs page. This method calls the Registerroute() method which creates a route table.

What is layout in MVC Razor?
When the application requires maintaining a consistent look and feel across multiple view then we uses layout. It is just like a master page of the web application,  But it provides a greater flexibility and has a simple syntax as compared to asp.net web application. It is generally use to define a common template for our application.

What is ViewStart in MVC? Or What does _ViewStart.cshtml file in MVC?
In MVC, When  multiple or group of view uses the same layout it may get some redundant code which is harder to maintain. So, To remove this redundancy we uses _viewstart.cshtml page. because this file executed before the any view page in directory. And a view can override the layout property and choose another property.  If a set of views shares common settings, the _ViewStart.cshtml file is a useful place to consolidate these common view settings. If any view needs to override any of the common settings, the view can set those values to another value.

What is NonActionAttribute in MVC?
It is already known that all the public method of a controller treated as action method. So, if you don’t want this default behavior then you can change public method with NonActionAttribute.

What is difference between Asp.net MVC and web form?
There are many difference of MVC and Web Form which are as follows :-
1. Web form follows an event driven development where as MVC follows Model View Controller.
2. Web page has server control wher as mvc has html helper.
3. Web form has state management technique where as mvc has not.
4. Web form has file based url where as mvc has route based url.
5. Web form uses masterpage for look and feel but MVC uses layout.
6. For reuseablity web form has a user control but MVC has partial View.

What is partial view in Asp.Net Mvc?
A partial view is just like as user control in Asp.Net web form. It is generally uses for the purpose of reusability.  Partial views are reusable views like as Header and Footer. It has .csHtml extension.
Partial and RenderPartial Html method renders partial view in MVC.s

<div>@Html.Partial("_Comments")</div>
<div>@ { Html.RenderPartial("_Comments") } </div>


What is the name of assembly where MVC framework defined?
System.Web.Mvc

Is it possible to combine ASP.NET webforms and ASP.MVC and develop a single web application?
Yes,

Is it possible to unit test an MVC application without running the controllers in an ASP.NET process?
Yes, all the features in an asp.net MVC application are interface based and hence mocking is much easier. So, we don't have to run the controllers in an ASP.NET process for unit testing.

Is it possible to share a view across multiple controllers?
Yes, put the view into the shared folder. This will automatically make the view available across multiple controllers.

Name the Some of the Return type of a controller Action .
1. ViewResult
2. PartialView Result
3. JavaScript Result
4. Json Result
5. Content Result
6. Redirect Result
7. File Result
8. Empty Result

Explain the advantages of using routing in ASP.NET MVC ?
Without using Routing in an ASP.NET MVC application, the incoming browser request should be mapped to a physical file.The thing is that if the file is not there, then you will get a page not found error.
By using Routing, it will make use of URLs where there is no need of mapping to specific files in a web site.
This is because, for the URL, there is no need to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

Explain with examples for scenarios when routing is not applied ?
Below are the 2 scenarios where routing is not applied.
i) A Physical File is Found that Matches the URL Pattern - This default behaviour can be overriden by setting the RouteExistingFiles property of the RouteCollection object to true.
ii) Routing Is Explicitly Disabled for a URL Pattern - By using the RouteCollection.Ignore() method, you can prevent routing from handling certain requests.

What are the 3 things that are needed to specify a route?
1. URL Pattern - You can include placeholders in a URL pattern so that variable data can be passed to the request handler without requiring a query string.
2. Handler - The handler can be a physical file such as an .aspx file or a controller class.
3. Name for the Route - Name is optional.

What is the difference between adding routes, to a webforms application and to an mvc application?
To add routes to a webforms application, we use MapPageRoute() method of the RouteCollection class, where as to add routes to an MVC application we use MapRoute() method.

Explain the usage of action filters in an MVC application ?
Action filter in an mvc application is used to perform some additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method.
Action filter is an attribute that implements the abstract FilterAttribute class. Or we also can say that, Action Filters allow us to add pre-action and post-action behavior to controller action methods.

If I have multiple filters impleted, what is the order in which these filters get executed?
1. Authorization filters
2. Action filters
3. Response filters
4. Exception filters

What are sections?
Layout pages, can define sections, which can then be overriden by specific views making use of the layout. Defining and overriding sections is optional.

No comments:

Post a Comment