Friday, December 19, 2014

MVC Interview Questions and Answers

Model View Controller


By reading these MVC interview question it does not mean you will go and clear MVC interviews. The whole purpose of this Post is to quickly brush up your MVC knowledge before you for the MVC interviews.
Before Going for Question Answers, i will give you a little bit of introduction about MVC.

What is MVC?

MVC is a framework pattern that splits an application’s implementation logic into three component roles: models, views, and controllers.
  • Model: The business entity on which the overall application operates. Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the data access layer because it is understood to be encapsulated by the Model.
  • View: The user interface that renders the Model into a form of interaction.
  • Controller: Handles a request from a View and updates the Model that results in a change of the Model’s state.To implement MVC in .NET, we need mainly three classes (ViewController and the Model).

    Explain MVC Architecture?

  •              The architecture is self explanatory. The browser (as usual) sends a request to IIS, IIS searches for the route defined in MVC application and passes the request to the controller as per route, the controller communicates with the model and passes the populated model (entity) to View (front end), Views are populated with model properties, and are rendered on the browser, passing the response to browser through IIS via controllers which invoked the particular View.

  Features of MVC 2.0

  • Introduction of UI helpers with automatic scaffolding with customizable templates
  • Attribute-based model validation on both client and server
  • Strongly typed HTML helpers
  • Improved Visual Studio tooling
  • There were also lots of API enhancements and “pro” features, based on feedback from developers building a variety of applications on ASP.NET MVC 1.0, such as:
    • Support for partitioning large applications into areas
    • Asynchronous controllers support
    • Support for rendering subsections of a page/site using Html.RenderAction
    • Lots of new helper functions, utilities, and API enhancements

  Features of MVC 3.0

  • The Razor view engine
  • Support for .NET 4 Data Annotations
  • Improved model validation
  • Greater control and flexibility with support for dependency resolution and global action filters
  • Better JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding
  • Use of NuGet to deliver software and manage dependencies throughout the platform

  Features of MVC 4.0

  • ASP.NET Web API
  • Enhancements to default project templates
  • Mobile project template using jQuery Mobile
  • Display Modes
  • Task support for Asynchronous Controllers
  • Bundling and minification

  Advantages of MVC

  1. Provides a clean separation of concerns between UI (Presentation layer), model (Transfer objects/Domain Objects/Entities) and Business Logic (Controller)
  2. Easy to UNIT Test
  3. Improved reusability of views/model. One can have multiple views which can point to the same model and vice versa
  4. Improved structuring of the code

  Explain Routing in MVC

A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.
Routing within the ASP.NET MVC framework serves two main purposes:
  • It matches incoming requests that would not otherwise match a file on the file system and maps the requests to a controller action.
  • It constructs outgoing URLs that correspond to controller actions.

  Now Ready for Question – Answers:

1. What are the 3 main components of an ASP.NET MVC application?
1. M – Model
2. V – View
3. C – Controller
2. In which assembly is the MVC framework defined?
System.Web.Mvc
3. 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.
4. What does Model, View and Controller represent in an MVC application?
Model: Model represents the application data domain. In short the applications business logic is contained with in the model.
View: Views represent the user interface, with which the end users interact. In short the all the user interface logic is contained with in the UI.
Controller: Controller is the component that responds to user actions. Based on the user actions, the respective controller, work with the model, and selects a view to render that displays the user interface. The user input logic is contained with in the controller.
5. 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.
6. Which approach provides better support for test driven development – ASP.NET MVC or ASP.NET Webforms?
ASP.NET MVC
7. 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 donot use viewstate.
8. 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.
9.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.
10. 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.
11.Where are the routing rules defined in an asp.net MVC application?
In Application_Start event in Global.asax
12. 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
13.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.
14. 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.
15. 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
Example: http://dotnetternikhil.com/Nikhil/Home/1
Controller Name = Nikhil
Action Method Name = Home
Parameter Id = 1
16. ASP.NET MVC application, makes use of settings at 2 places for routing to work correctly. What are these 2 places?
1. Web.Config File : ASP.NET routing has to be enabled here.
2. Global.asax File : The Route table is created in the application Start event handler, of the Global.asax file.
17.What is the adavantage of using ASP.NET routing?
In an ASP.NET web application that does not make use of routing, an incoming browser request should map to a physical file. If the file does not exist, we get page not found error.
An ASP.NET web application that does make use of routing, makes use of URLs that do not have to map to specific files in a Web site. Because the URL does not have 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.
18. 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.
19. Is the following route definition a valid route definition?
{controller}{action}/{id}
No, the above definition is not a valid route definition, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the controller placeholder from the value for the action placeholder.
20. What is the use of the following default route?
{resource}.axd/{*pathInfo}
This route definition, prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.
21. 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.
22. How do you handle variable number of segments in a route definition?
Use a route with a catch-all parameter. An example is shown below. * is referred to as catch-all parameter.
controller/{action}/{*parametervalues}
23. What are the 2 ways of adding constraints to a route?
1. Use regular expressions
2. Use an object that implements IRouteConstraint interface
24. Give 2 examples for scenarios when routing is not applied?
1. 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.
2. Routing Is Explicitly Disabled for a URL Pattern – Use the RouteCollection.Ignore() method to prevent routing from handling certain requests.
25. What are the 2 popular asp.net mvc view engines?
1. Razor
2. .aspx
26. What symbol would you use to denote, the start of a code block in razor views?
@
27. What symbol would you use to denote, the start of a code block in aspx views?
<%= %>

0 comments:

Post a Comment