Top 36 Mvc Framework Interview Questions You Must Prepare 19.Mar.2024

MVC is a software architecture pattern for developing web application. It is handled by three objects Model-View-Controller.

The filter order would be like
•Authorization filters
•Action filters
•Response filters
•Exception filters

Session can be maintained in MVC by three ways tempdata, viewdata, and viewbag.

•View Result
•Javascript Result
•Redirect Result
•Json Result
•Content Result

To add routes to a webform application, we can use MapPageRoute() method of the RouteCollection class, where adding routes to an MVC application, you can use MapRoute() method.

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

All public methods of a controller class are treated as the action method if you want to prevent this default method then you have to assign the public method with NonActionAttribute.

Routing helps you to decide a URL structure and map the URL with the Controller.
The three segments that are important for routing is
•ControllerName
•ActionMethodName
•Parameter

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

In MVC, there are twelve types of results in MVC where “ActionResult” class is the main class while the 11 are their sub-types
•ViewResult
•PartialViewResult
•EmptyResult
•RedirectResult
•RedirectToRouteResult
•JsonResult
•JavaScriptResult
•ContentResult
•FileContentResult
•FileStreamResult
•FilePathResult

•MVC segregates your project into a different segment, and it becomes easy for developers to work on.
•It is easy to edit or change some part of your project that makes project less development and maintenance cost.
•MVC makes your project more systematic.

The two methods to add constraints to a route is
•Use regular expressions
•Use an object that implements IRouteConstraint Interface

The steps for the execution of an MVC project includes
•Receive first request for the application
•Performs routing
•Creates MVC request handler
•Create Controller
•Execute Controller
•Invoke action
•Execute Result

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

•Presentation: It is the visual representation of a specific abstraction within the application
•Abstraction: It is the business domain functionality within the application
•Control: It is a component that keeps consistency between the abstraction within the system and their presentation to the user in addition to communicating with other controls within the system

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 MVVM is more suitable due to bindings.

For razor views the file extensions are
•.cshtml: If C# is the programming language
•.vbhtml: If VB is the programming language

The MVC framework is defined in System.Web.Mvc.

In order to send the result back in JSON format in MVC, you can use “JSONRESULT” class.

Two methods for adding constraints to route is
•Using regular expressions
•Using an object that implements IRouteConstraint interface

Partial view in MVC renders a portion of view content. It is helpful in reducing code duplication. In simple terms, partial view allows to render a view within the parent view.

There are two key benefits of using MVC
•As the code is moved behind a separate class file, you can use the code to a great extent
•As behind code is simply moved to.NET class, it is possible to automate UI testing. This gives an opportunity to automate manual testing and write unit tests.

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 bindingcode 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

View :

  • It contains the layout page
  • Before any view is rendered, viewstart page is rendered
  • View might have markup tags like body, html, head, title, meta etc.
  • View is not lightweight as compare to Partial View

Partial View :

  • It does not contain the layout page
  • Partial view does not verify for a viewstart.cshtml. We cannot put common code for a partial view within the viewStart.cshtml.page
  • Partial view is designed specially to render within the view and just because of that it does not consist any mark up
  • We can pass a regular view to the RenderPartial method

In Ajax, MVC can be implemented in two ways
•Ajax libraries
•Jquery

In an MVC model,
•Model– It represents the application data domain. In other words applications business logic is contained within the model and is responsible for maintaining data
•View– It represents the user interface, with which the end users communicates. In short all the user interface logic is contained within the VIEW
•Controller– It is the controller that wers to user actions. Based on the user actions, the respective controller responds within the model and choose a view to render that display the user interface.  The user input logic is contained with-in the controller

•Temp data: It helps to maintain data when you shift from one controller to other controller.
•View data: It helps to maintain data when you move from controller to view
•View Bag: It’s a dynamic wrapper around view data

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

We can decorate the MVC action by “HttpGet” or “HttpPost” attribute to restrict the type of HTTP calls. For instanceyou can see in the below code snippet the “DisplayCustomer” action can only be invoked by “HttpGet”. If we try tomake Http post on “DisplayCustomer” it will throw an error.
[HttpGet]
public ViewResult DisplayCustomer(int id)
{
Customer objCustomer = Customers[id];
return View("DisplayCustomer",objCustomer);
}

This default route prevents request for a web resource file such as Webresource.axd or ScriptResource.axd from being passed to the controller.

Two instance where routing is not required are
•When a physical file is found that matches the URL pattern
•When routing is disabled for a URL pattern

There is a group of routes called the RouteCollection, which consists of registered routes in the application.The RegisterRoutes method records the routes in this collection.  A route defines a URL pattern and a handler to use if the request matches the pattern. The first parameter to the MapRoute method is the name of the route. The second parameter will be the pattern to which the URL matches.  The third parameter might be the default values for the placeholders if they are not determined.

In the end “Exception Filters” are executed.

Advantages:

  • It represents clear separation between business logic and presentation logic
  • Each MVC object has different responsibilities
  • The development progresses in parallel
  • Easy to manage and maintain
  • All classes and object are independent of each other

Disadvantages:

  • The model pattern is little complex
  • Inefficiency of data access in view
  • With modern user interface, it is difficult to use MVC
  • You need multiple programmers for parallel development
  • Multiple technologies knowledge is required

 

 

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.
@ViewResult
@JavaScriptResult
@RedirectResult
@ContentResult
@JsonResult

 

Sessions can be maintained in MVC by 3 ways tempdata ,viewdata and viewbag