MVC interview questions

MVC interview questions

MVC is an abbreviation for Model, View, and Controller. The MVC architectural pattern separates an application into three components – Model, View, and Controller. In this pattern, the model represents the shape of the data and business logic. It maintains and preserves the data of the application. Model objects retrieve and store model state in a database. The view is basically and technically a user interface. The view segment displays the data-using model to the user and also enables them to modify the data. The controller is the part, which handles the user request.

Typically, a user interacts with View, which in turn raises appropriate URL requests. Finally, a controller will handle this request. The controller segment of the MVC renders the appropriate view with the model data as a response. Implementing this structure in applications and creating web apps based on this ASP.NET framework in Azure and other platforms is what most developers ought to know.

However, one might forget the basics when dealing with practical problems on a regular basis. So, here is a list of common interview questions on the basics of MVC, which might be asked in an interview for any post or designation.

Download MVC interview questions PDF

MVC interview questions

The main objective of making use of Output Caching is to dramatically improve the performance of an ASP.NET MVC Application. It enables us to cache the content returned by any controller method so that the same content does not need to be generated each time the same controller method is invoked. Output Caching has huge advantages, such as it reduces server round trips, reduces database server round trips, reduces network traffic, etc.

Following are the rules for main Razor Syntax:

  • Razor code blocks are enclosed in @{ … }
  • Inline expressions (variables and functions) start with @
  • Code statements end with a semicolon
  • Variables are declared with the var keyword
  • Strings are enclosed with quotation marks
  • C# code is case sensitive
  • C# files have the extension .cshtml

 A partial view is a chunk of HTML that can be safely inserted into an existing DOM. Most commonly, partial views are used to componentize Razor views and make them easier to build and update. Partial views can also be returned directly from controller methods. In this case, the browser still receives text/HTML content but not necessarily HTML content that makes up an entire page. As a result, if a URL that returns a partial view is directly invoked from the address bar of a browser, an incomplete page may be displayed. This may be something like a page that misses title, script and style sheets.

GET Action Type: GET is used to request data from a specified resource. With all the GET requests, we pass the URL, which is compulsory; however, it can take up the following overloads.

POST Action Type: Tthe POST is used to submit data to be processed to a specified resource. With all the POST requests, we pass the URL, which is essential and the data. However, it can take up the following overloads. 

ASP.NET MVC structure upholds the following action filters:

  • Action Filters: Action filters are used to implement logic that gets executed before and after a controller action executes. We will look at Action Filters in detail in this chapter.
  • Authorization Filters: Authorization filters are used to implement authentication and authorization for controller actions.
  • Result Filters: Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
  • Exception Filters: Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You can also use exception filters to log errors.

 In MVC, controllers define action methods and these action methods generally have a one-to-one relationship with UI controls such as clicking a button or a link, etc. For example, in one of our previous examples, the UserController class contained methods UserAdd, UserDelete, etc. However, the users would often like to perform some action before or after a particular operation. 

 Database First Approach is an alternative or substitutes to the Code First and Model First approaches to the Entity Data Model. The Entity Data Model creates model codes (classes, properties, DbContext, etc.) from the database in the project and that class behaves as the link between database and controller.

There are the following approaches, which are used to connect the database with the application.

  • Database First
  • Model First
  • Code First

Benefits or advantages of MVC are as follows:

  • Multiple view support: Due to the separation of the model from the view, the user interface can display multiple views of the same data at the same time.
  • Change Accommodation: User interfaces tend to change more frequently than business rules (different colors, fonts, screen layouts, and levels of support for new devices such as cell phones or PDAs)
  • SoC – Separation of Concerns: Separation of Concerns is one of the core advantages of ASP.NET MVC. The MVC framework provides a clean separation of the UI, Business Logic, Model or Data.
  •  More Control: The ASP.NET MVC framework provides more control over HTML, JavaScript, and CSS than the traditional Web Forms.
  • Testability: ASP.NET MVC framework provides better testability of the Web Application and good support for the test-driven development too.
  •   Lightweight: ASP.NET MVC framework doesn’t use View State and thus reduces the bandwidth of the requests to an extent.

 Action Filters are additional attributes that can be applied to either a controller section or the entire controller to modify the way in which action is executed. These attributes are special .NET classes derived from system attributes, which can be attached to classes, methods, properties, and fields.

 In ASP.NET MVC there are three ways to pass/store data between the controllers and views.

ViewData

  1. ViewData is used to pass data from a controller to view.
  2. It is derived from ViewDataDictionary class.
  3. It is available for the current request only.
  4. Requires typecasting for complex data type and checks for null values to avoid error.
  5. If redirection occurs, then its value becomes null.

ViewBag

  1. ViewBag is also used to pass data from the controller to the respective view.
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
  3. It is also available for the current request only.
  4. If redirection occurs, then its value becomes null.
  5. Doesn’t require typecasting for the complex data type.

 The Validation Summary helper method generates an unordered list (UL element) of validation messages that are in the Model State Dictionary object.

The Validation Summary can be used to display all the error messages for all the fields. It can also be used to display custom error messages. The following figure shows how Validation Summary displays the error messages.

 As the name implies, attribute routing uses attributes to define routes. In ASP.NET MVC 5.0 we have a new attribute route. By using the “Route” attribute we can define the URL structure. For instance, in any code, a user can decorate the “GotoAbout” action with the route attribute. The route attribute says that the “GotoAbout” can be invoked using the URL structure “Users/about”. Attribute routing gives the user more control over the URLs in their web application.

 Routing is a great feature of MVC because it provides a REST-based URL which is very easy to remember. It also improves page ranking in search engines.

This article is not an introduction to Routing in MVC, but we will learn a few features of routing, and by implementing them we can develop a very flexible and user-friendly application. So, let’s start without wasting valuable time.

The MVC model defines web applications with 3 logic layers:

  • The business layer (Model logic)
  • The display layer (View logic)
  • The input control (Controller logic)

The Model is the part of the application, which only handles the logic for the application data. Regularly, the model objects retrieve data (as well as store data) from a database. The View is the part of the application, which takes care of the display of the data.

Most often, the views are created from the model data, although there are other, more complicated methods of creating views.

The Controller, as the name implies, is the part of the application that handles user interaction.

 ASP.NET MVC has always supported the concept of “view engines” – which are the pluggable modules, which practically implement different template syntax options. The “default” view engine for ASP.NET MVC uses the same .aspx/.ascx/. master file templates as ASP.NET Web Forms. Other popular ASP.NET MVC view engines are Spart&Nhaml. Razor is the new view engine introduced by MVC 3.