Monday 22 July 2013

MVC Interview questions and answers


About ASP.Net MVC

The ASP.Net MVC is the framework provided by Microsoft that lets you develop the applications that follows the principles of Model-View-Controller (MVC) design pattern.
The .Net programmers new to MVC thinks that it is similar to WebForms Model (Normal ASP.Net), but it is far different from the WebForms programming. This article will tell you how to quick learn the basics of MVC along with some frequently asked interview questions and answers on ASP.Net MVC

1. What are the 3 main components of an ASP.NET MVC application?

1. M - Model
2. V - View
3. C – Controller


2. Can you explain the complete flow of MVC? 

Below are the steps how control flows in MVC architecture:-
1.       All end user requests are first sent to the controller.
2.       The controller depending on the request decides which model to load. The controller loads the model and attaches the model with the appropriate view.
3.       The final view is then attached with the model data and sent as a response to the end user on the browser.

3.  In which assembly is the MVC framework defined?

System.Web.Mvc

4.  Is MVC suitable for both windows and web application?

MVC architecture is suited for web application than windows. For window application MVP i.e. “Model view presenter” is more   applicable. If you are using WPF and SL and MVVM is more suitable due to bindings.

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

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

6.  What are the benefits of using MVC?

There are two big benefits of MVC:-
Separation of concerns is achieved as we are moving the code behind to a separate class file. By moving the binding code to a separate class file we can reuse the code to a great extent.
Automated UI testing is possible because now the behind code (UI interaction code) has moved to a simple.NET class. This gives us opportunity to write unit tests and automate manual testing.

7.  What is the greatest advantage of using asp.net mvc over asp.net webforms?

It is difficult to unit test UI with webforms, where views in mvc can be very easily unit tested.

8.  Which approach provides better support for test driven development - ASP.NET MVC or ASP.NET Webforms?

ASP.NET MVC

9.  What are the advantages of ASP.NET MVC?

1. Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested.
2. Complex applications can be easily managed
3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller.
4. ASP.NET MVC views are light weight, as they do not use viewstate.

10.  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.

11.  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.

12.  What is the role of a controller in an MVC application?

The controller responds to user interactions, with the application, by selecting the action method to execute and alse selecting the view to render.

13.  Where are the routing rules defined in an asp.net MVC application?

In Application_Start event in Global.asax

14.  Name a few different return types of a controller action method?

The following are just a few return types of a controller action method. In general an action method can return an instance of a any class that derives from ActionResult class.
1. ViewResult
2. JavaScriptResult
3. RedirectResult
4. ContentResult
5. JsonResult

15.  What is the significance of NonActionAttribute?

In general, all public methods of a controller class are treated as action methods. If you want prevent this default behaviour, just decorate the public method with NonActionAttribute.

16.  What is the significance of ASP.NET routing?

ASP.NET MVC uses ASP.NET routing, to map incoming browser requests to controller action methods. ASP.NET Routing makes use of route table. Route table is created when your web application first starts. The route table is present in the Global.asax file.

17.  What are the 3 segments of the default route, that is present in an ASP.NET MVC application?

1st Segment - Controller Name
2nd Segment - Action Method Name
3rd Segment - Parameter that is passed to the action method


18.  What is the latest version of MVC? 

When this note was written, four versions where released of MVC. MVC 1 , MVC 2, MVC 3 and MVC 4. So the latest is MVC 4. 

19.  Where is the route mapping code written?

The route mapping code is written in the “global.asax” file.

20.  What are routing in MVC? 

Routing helps you to define a URL structure and map the URL with the controller.
For instance let’s say we want that when any user types “http://localhost/View/ViewCustomer/”,  it goes to the  “Customer” Controller  and invokes “DisplayCustomer” action.  This is defined by adding an entry in to the “routes” collection using the “maproute” function. Below is the under lined code which shows how the URL structure and mapping with controller and action is defined.
routes.MapRoute(
               "View", // Route name
               "View/ViewCustomer/{id}", // URL with parameters
               new { controller = "Customer", action = "DisplayCustomer", 
id = UrlParameter.Optional }); // Parameter defaults

21.  Can we map multiple URL’s to the same action?

Yes , you can , you just need to make two entries with different key names and specify the same controller and action.

22.  How can we navigate from one view to other view using hyperlink?

By using “ActionLink” method as shown in the below code. The below code will create a simple URL which help to navigate to the “Home” controller and invoke the “GotoHome” action.
<%= Html.ActionLink("Home","Gotohome") %>

23.  What is razor in MVC? 

It’s a light weight view engine. Till MVC we had only one view type i.e.ASPX, Razor was introduced in MVC 3.

24.  So which is a better fit Razor or ASPX?

As per Microsoft razor is more preferred because it’s light weight and has simple syntaxes.

25.  How can you do authentication and authorization in MVC?

You can use windows or forms authentication for MVC.


No comments:

Post a Comment