MVC Interview Questions and Answers For 2 Years Experience
by Anuradha, on Mar 26, 2018 2:18:18 PM
Q1. What do you meant MVC?
Ans: 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 (View, Controller and the Model).
Q2. What is meant by ASP.Net MVC
Ans: The ASP.Net MVC is the framework provided by Microsoft to achieve separation of concerns that leads to easy maintainability of the application.
Model is supposed to handle data related activity
View deals with user interface related work
Controller is meant for managing the application flow by communicating between Model and View.
Q3. What are the new features of MVC2?
Ans: ASP.NET MVC 2 was released in March 2010. Its main features are:
- 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, such as:
- Support for partitioning large applications into
- Asynchronous controllers support.
- Support for rendering subsections of a page/site using Html.RenderAction.
- Lots of new helper functions, utilities, and API enhancements
Q4. Explain “page lifecycle” of an ASP.NET MVC?
Ans: Following process are performed by ASP.Net MVC page:
- App initialization
- Routing
- Instantiate and execute controller
- Locate and invoke controller action
- Instantiate and render view
Q5. What are the new features of MVC3?
Ans: ASP.NET MVC 3 shipped just 10 months after MVC 2 in Jan 2011.Some of the top features in MVC 3 included:
- 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.
Q6. What are the new features of MVC4?
Ans: Following are the top features of MVC4:
- NET Web API.
- Enhancements to default project templates.
- Mobile project template using jQuery Mobile.
- Display Modes.
- Task support for Asynchronous Controllers.
- Bundling and minification.
Q7. Why to use ASP.Net MVC
Ans: The strength of MVC (i.e. ASP.Net MVC) is listed below, that will answer this question
MVC reduces the dependency between the components; this makes your code more testable.
MVC does not recommend use of server controls, hence the processing time required to generate HTML response is drastically reduced.
The integration of java script libraries like jQuery, Microsoft MVC becomes easy as compared to Webforms approach.
Q8. Advantages of MVC Framework?
- Provides a clean separation of concerns between UI (Presentation layer), model (Transfer objects/Domain Objects/Entities) and Business Logic (Controller).
- Easy to UNIT Test.
- Improved reusability of views/model. One can have multiple views which can point tosame model and vice versa.
- Improved structuring of the code.
Q9. What do you mean by Razor
Ans: The Razor is the new View engine introduced in MVC 3.0.
The View engine is responsible for processing the view files [e.g. .aspx, .cshtml] in order to generate HTML response.
The previous versions of MVC were dependent on ASPX view engine.
Q10. What do you mean by Separation of Concerns?
Ans: As per Wikipedia 'the process of breaking a computer program into distinct features that overlap in functionality as little as possible'. MVC design pattern aims to separate content from presentation and data-processing from content.
Q11. Can we use ASPX view engine in latest versions of MVC
Ans: Yes. The Recommended way is to prefer Razor View
Q12. Where do we see Separation of Concerns in MVC?
Ans: Between the data-processing (Model) and the rest of the application.
When we talk about Views and Controllers, their ownership itself explains separation. The views are just the presentation form of an application, it does not have to know specifically about the requests coming from controller. The Model is independent of View and Controllers, it only holds business entities that can be passed to any View by the controller as required for exposing them to the end user. The controller is independent of Views and Models, its sole purpose is to handle requests and pass it on as per the routes defined and as per the need of rendering views. Thus our business entities (model), business logic (controllers) and presentation logic (views) lie in logical/physical layers independent of each other.
Q13. What are the benefits of Razor View?
Ans: The syntax for server side code is simplified
The length of code is drastically reduced
Razor syntax is easy to learn and reduces the complexity
Q14. What is the extension of Razor View file?
Ans: .cshtml (for c#) and .vbhtml (for vb)
Q15. What are Display Modes in MVC4?
Ans: Display modes use a convention-based approach to allow selecting different views based on the browser making the request. The default view engine fi rst looks for views with names ending with .Mobile.cshtml when the browser’s user agent indicates a known mobile device. For example, if we have a generic view titled Index.cshtml and a mobile view titled Index.Mobile.cshtml, MVC 4 will automatically use the mobile view when viewed in a mobile browser.
Additionally, we can register your own custom device modes that will be based on your own custom criteria — all in just one code statement. For example, to register a WinPhone device mode that would serve views ending with .WinPhone.cshtml to Windows Phone devices, you’d use the following code in the Application_Start method of your Global.asax:
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("WinPhone")
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf
("Windows Phone OS", StringComparison.OrdinalIgnoreCase) >= 0)
});
Q16. How to create a Controller in MVC
Ans: Create a simple class and extend it from Controller class. The bare minimum requirement for a class to become a controller is to inherit it from ControllerBase is the class that is required to inherit to create the controller but Controller class inherits from ControllerBase.
Q17. What is AuthConfig.cs in MVC4?
Ans: AuthConfig.cs is used to configure security settings, including sites for OAuth login.
Q18. How to create an Action method in MVC
Ans: Add a simple method inside a controller class with ActionResult return type.
Q19. How to access a view on the server
Ans: The browser generates the request in which the information like Controller name, Action Name and Parameters are provided, when server receives this URL it resolves the Name of Controller and Action, after that it calls the specified action with provided parameters. Action normally does some processing and returns the ViewResult by specifying the view name (blank name searches according to naming conventions).
Q20. What is the default Form method (i.e. GET, POST) for an action method
Ans: GET. To change this you can add an action level attribute e.g [HttpPost]