MVC Interview Questions

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

If you are expecting a .NET interview and want to prepare for ASP.NET MVC interview questions, we are here to help you out in passing your upcoming interview.

1. What is MVC?

MVC stands for Model View Controller, which is a software architectural pattern to develop web applications. It comes as a part of .NET framework and simplifies the development of building web applications. It separates the front layer from the back end layer. Multiple developers can work on different layers simultaneously as it divides the application into three main layers:

1.Model Layer

The Model Layer is responsible for the application logics. It is often used to retrieve or send the data to the database.

2.Controller Layer

The Controller Layer is responsible to manage the data between View and Model layers. It acts as a bridge between model and view layer i.e. it reads the data from the view and send it back to the model layer.

3.View Layer

The View Layer or the presentation layer displays the data to the connected user. It is created with the help of models or entities.

2. What are the advantages of MVC?

It has many advantages over the ASP.NET language. Some of them are given below:

1. Separation of Concerns – SoC

Separation of Concern is one of the main advantages of the MVC framework. It is a design principle that separates the program logics into different sections and each section is responsible for its own handling of program logic independently.

2. Support Multiple Views

In MVC, model can be bind with multiple views simultaneously and a view can have multiple views at the same time.

3. Front End Programming Control

In ASP.NET MVC, you can have more control over HTML, JavaScript, and CSS unlike the traditional web forms or applications.

4. SEO Friendly

MVC supports the development of web applications that are more SEO friendly

5. Rapid Development

It supports faster and rapid development by allowing us to work in parallel.

3. Explain MVC Application Life Cycle

Typically, any web application has two main steps, to send a request to the server and receive a response based on the type of the request nature. The MVC application also follow the same life cycle i.e. send a request to the server from the connected client and server receive the request, executes the request and send back the response to the client.

4. How do you create Request Object in MVC?

There are four steps to create Request Object:

  1. Fill Route

The first step in creating a request object is to fill the route. All the requests are mapped to the route tables in MVC that invokes the specific controller and action.

  1. Fetch Route

In this step, Url Routing Module searches the route table according to the request and create the Route Data object.

  1. Create Request Context

After fetching the requested method and action, request context is created.

  1. Create Controller Instance

Once the request context is created, the request object is sent to the MVC Handler to create the instance of controller. After that, it calls the “Execute” method of the controller.

5. How do you maintain the sessions in MVC?

There are three possible ways to create and manage the session in MVC:

  • View Data
  • Temp Data
  • View Bag

6. Explain the difference between ViewData, Temp Data and ViewBag ?

View Data

  • It is a Key Value pair dictionary collection
  • It is faster than View Bag
  • It was introduced in MVC 1.0
  • It works with .NET Framework 3.5 and above
  • It has a scope during the current request
  • Type conversion is required before accessing the data

View Bag

  • It is a type object
  • It is slower than View Data
  • It was introduced in MVC 3
  • It works with .NET framework 4.0 and above
  • It has a scope during the current request
  • There is no need for type conversion as it is dynamic

Temp Data

  • It is a Key Value Dictionary Collection
  • It was introduced in MVC 1.0
  • It works with .NET framework 3.5 and above
  • It has a scope during the current and subsequent request
  • It is used to send data between two consecutive requests
  • Type conversion is required before accessing the data

7. Give an example of using TempData in MVC Controller.


// MVC Controller that returns a view
[HttpGet] 
public ActionResult Student()
{
    List studentList = new List ();
    studentList.Add(" Student A ");
    studentList.Add(" Student B ");
    studentList.Add(" Student C");

    TempData["StudentList"] = studentList ;
    return View() ;
}

<ul>
@foreach ( var student in TempData["StudentList"] as List<string> )
{ 
<li> Student Name : @student </li>
} 
</ul>

8.How do you set data in ViewBag and View Data?

You can set the data in the ViewBag and ViewData in the following manner:

// student list
List studentList = new List ();
studentList.Add(" Student A ");
studentList.Add(" Student B ");
studentList.Add(" Student C");

// Setting list in ViewData
ViewData["Student"] = Student;

// Setting list in ViewBag
ViewBag.Student = Student;

9. What are Filters in MVC?

The ASP.NET MVC provides a feature to add a pre and post functionality to the execution of requests. Generally actions are performed one to one and at time you need to add logics before executing those actions such as checking user session before saving the new record to the database.

What are the different types of Filters?

MVC supports the following filters:

  • Action Filters
  • Authorization Filters
  • Result Filters
  • Exception Filters

10. Why Action Filters are used in MVC?

The Action Filters are additional attributes that can be added to the controller or action method and are used to execute the logic before and after the execution of action methods. These action filters are specialized filters that are available in System.Attribute namespace.

11. What is Routing?

Routing is the mechanism of mapping incoming requests from the browsers to the MVC controller action method.

12. How many segment are there in MVC Routing?

There are the main segments in routing that are given here:

  • Controller Name
  • Action Method Name
  • Parameter

13. Explain the properties of MVC Routes

The properties of MVC routes are given below:

  • Route Name

It is used to map the handler of URL Pattern

  • URL Pattern

It is also called as URL Parameters having literal values of variables

  • Defaults

It is used to assign default parameter value at the time of creating a parameter

  • Constraints

These are used to apply constraints against the URL

14. How to navigate from one view to another view by using hyperlink?

You can use Action Link method to navigate from one view to another. Let suppose you are at Home page and you want to go to your Profile page, you can simple do this with the following code:

<%= Html.ActionLink( "Profile", "My Profile " ) %>/* Your code... */

15. What is Minification?

Minification removes all the whitespaces and renames the variables to the shortest names wherever it is required. It improves the request process time by making the file less in size to download over the network. It can be implemented in the application either by setting the value in web config file or enable the property on bundle classes.

16. What is Bundling?

Bundling allows you to request a bunch of files from the server in a single request. This technique in introduced in MVC 4.0 to improve the processing time by eliminating the multiple request overhead.

17. What is Scaffolding?

Scaffolding is an advanced technique used by many popular frameworks to generate programming code for basic CRUD operations, which includes create, read, update and delete against the database. This auto-generated code can be customized or modified according to the needs of an application.

18. What is Code First Approach in MVC?

In Code First Approach, the entity framework will create or update the databases. Thus, you need to write the code first to create the database and once the program in written, then the database will eb created or updated.

19. How do you implement MVC Forms Authentication?

You can add the forms authentication to your web application in order to add more security in it. It can be implemented with the help of following code:

<system.web> 
<authentication mode = "Forms" > 
<formsloginUrl = "Login.aspx" protection = "All" timeout = "30" name = ".ASPXAUTH" path = "/" requireSSL = "false" 
                 defaultUrl = "default.aspx" cookieless = "UseDeviceProfile" /> 
</authentication> 
</system.web>

20. What is Razor in ASP.NET MVC?

Razor is markup syntax and is used to create dynamic web pages with server side language i.e. C# or VB.NET. It is easier to learn and is not tied with ASP.NET MVC only. It is used to produce HTML code as an output.

21. What are the extensions used for Razor Views?

  • .cshtml – This extension is used for C# programming language
  • .vbhtml – This extension is used for VB programming language

22. What are the rules of Razor syntax?

There are following rules that must be followed to write razor code:

  • You must use block of Razor codes enclosing within @{ you code here }
  • You can access the variables and functions of inline expressions that starts with @ symbol
  • You can use the ‘var’ keyword to declare variables
  • You must use terminator or semi colon to terminate the line of code in razor also
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.