MVC or Model-View-Controller is a software design pattern that is used to conceptualize, frame, implement, commission, and ultimately modify the complex systems. MVC is used for developing user interfaces that divide the logic of the program into three interconnected elements. This separation of internal representations of the information is done to distinguish how the information is presented and accepted by the user.
Here is a list of the top 20 MVC Interview Questions:
Question 1. Explain Model-View-Controller.
MVC or Model-View-Controller is a software design pattern that is used for developing web applications, and three Model-View-Controller objects handle the operations.
Question 2. What is the function of Model-View-Controller in an MVC application?
Model- Model represents the applications business logic that is present in the model and takes up the responsibility of maintaining data.
View- It represents the interface with which the user communicates.
Controller- It reacts to the user’s actions and acts depending on the choices made by the user and responds within the model. The input logic is present in the controller.
Question 3. In which assembly is the MVC framework designed?
The MVC framework is defined in System.Web.Mvc.
Question 4. List a few return types of the controller action method.
- View Result
- Redirect Result
- Json Result
- Content Result
Question 5. List the advantages of MVC.
Advantages of MVC are:
- Multiple View Support: The separation of the model from view can allow a user to display many views of the same data at the same time.
- Lightweight: MVC framework is easy to use, and as it does not use View State, it reduces the bandwidth of requests.
- Change Accommodation: The user interface frequently changes in comparison to business rules.
- Testability: This framework allows better testability of Web Application and excellent support for test-driven development tools.
Question 6. How is a session maintained in MVC?
Sessions can be maintained in MVC in three ways temp data, viewdata, and view bag.
Question 7. What does “beforeFilter()”,”beforRender” and “afterFilter” functions do?
- beforeFilter(): This function is used before every action in the controller, and it is used to check the active session or inspect user permissions.
- beforeRender(): The function is the controller action logic before the view is rendered. The feature is not used often and is required when render() is called manually.
- afterFilter(): The function is called after controller action, and once the rendering is done. This is the last method of the controller to be used.
Question 8. Explain role of Presentation, Abstraction, and Control in MVC.
Presentation is the visual representation of abstraction within the application. Abstraction is the domain functionality in the applications, and Control is the part that keeps the abstraction in the system, and the presentation to the user consistent.
Question 9. What is the role of “ActionFilters” in MVC?
In MVC ‘ActionFilters” help the user to execute logic while MVC action is completed or is running.
Question 10. Write the steps for the execution of the MVC project.
These are the steps for the execution of an MVC project:
- Receive request for application
- Perform routing
- Create MVC request handler
- Create the Controller
- Execute the Controller in application
- Invoke action
- Execute Result
Question 11. What is routing? Name three segments for which routing is essential.
Routing is the process that helps you decide a URL structure and map it with the controller. The three segments which are important for routing are:
Question 12. Explain Spring MVC.
Java framework follows the Model-View-Controller design pattern, and it is used to build web applications. It also implements the basic features of the core Spring framework, like Inversion of Control and Dependency Injection. Spring MVC enables a solution for the MVC in Spring Framework with the help of DispatcherServlet.
Question 13. List two ways to add constraints in a route.
The two ways by which we can add a constraint to the route are:
- using regular expressions in the application
- using the object that implements IRouteConstraint interface
Question 14. How to implement validation in MVC?
Validation can be executed easily in MVC application with the help of validators, which are defined in the System.ComponentModel.DataAnnotations namespace. There are several validators, as follows:
Question 15. Can we implement Ajax in MVC? If yes, in how many ways can we do it?
Yes, Ajax can be implemented in MVC, and it can be done in two ways. These are:
- Ajax Libraries
Question 16. Explain partial view in MVC.
To render a view within a parent view in MVC, a partial view is used. It helps in reducing code duplication during programming.
Question 17. What is Code Blocks in Views?
Instead of evaluating a single code at a time and sending a response, blocks of code are executed in the system. It is useful for declaring variables that can be used later in the process.
- int x = 123;
- string y = “aa”;
Question 18. What is better, Razor or ASPX?
According to Microsoft, Razor is preferred over ASPX because of its lightweight and simple syntax.
Question 19. What is Glimpse?
Glimpse is a NuGet package that is used to find performance, debugging, and diagnostic information. It can be used to get information about timelines, model binding, routes, etc.
Question 20. What are two cases when routing is not required?
The two cases in which routing is not required are:
- when a physical file matching the URL pattern is found
- when routing is not allowed to be done for a URL pattern