Expert Dot Net

Trust me to find new way !

ASP.NET MVC Interview Questions

Q1 - What is MVC (Model view controller)?

 Answer

Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representation of information from the way that information is presented to or accepted from the user.

MVC is a framework for building web applications using a MVC (Model View Controller) design:

The Model represents the application core (for instance a list of database records).

The View displays the data (the database records).

The Controller handles the input (to the database records).

The MVC model also provides full control over HTML, CSS, and JavaScript.




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 that handles the logic for the application data.

Often model objects retrieve data (and store data) from a database.



The View is the part of the application that handles the display of the data.

Most often the views are created from the model data.



The Controller is the part of the application that handles user interaction.

Typically controllers read data from a view, control user input, and send input data to the model.

The MVC separation helps you manage complex applications, because you can focus on one aspect a time. For example, you can focus on the view without depending on the business logic. It also makes it easier to test an application.

The MVC separation also simplifies group development. Different developers can work on the view, the controller logic, and the business logic in parallel.

 

Q2. Explain in which assembly is the MVC framework is defined?


Answer

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


Q4. List out few different return types of a controller action method?

Answer

View Result

Javascript Result

Redirect Result

Json Result

Content Result


Q5.  Mention what is the advantages of MVC?


Answer

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

 

Q6. Explain the role of components Presentation, Abstraction and Control in MVC?


Answer

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


Q7- Explain MVC application life cycle?


Answer

 Any web application has two main execution steps, first understanding the request and depending on the type of the request sending out appropriate response. MVC application life cycle is not different it has two main phases, first creating the request object and second sending our response to the browser.


Creating the request object,

The request object creation has four major steps. The following is the detailed explanation of the same.

Step 1 - Fill route

MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file.

Step 2 - Fetch route

Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke.

Step 3 - Request context created

The “RouteData” object is used to create the “RequestContext” object.

Step 4 - Controller instance created 

This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.

Creating Response object

This phase has two steps executing the action and finally sending the response as a result to the view.




Q8 - List out different return types of a controller action method?

 

Answer

 

There are total nine return types we can use to return results from controller to view.


The base type of all these result types is ActionResult.

ViewResult (View)
This return type is used to return a webpage from an action method.

PartialviewResult (Partialview)
This return type is used to send a part of a view which will be rendered in another view.

RedirectResult (Redirect)
This return type is used to redirect to any other controller and action method depending on the URL.

RedirectToRouteResult (RedirectToAction, RedirectToRoute)
This return type is used when we want to redirect to any other action method.

ContentResult (Content)
This return type is used to return HTTP content type like text/plain as the result of the action.

jsonResult (json)
This return type is used when we want to return a JSON message.

javascriptResult (javascript)
This return type is used to return JavaScript code that will run in browser.

FileResult (File)
This return type is used to send binary output in response.

EmptyResult
This return type is used to return nothing (void) in the result.

 

 

Q9. What is Razor in MVC?

Answer

 

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.

 

Q10. Explain what is Database First Approach in MVC using Entity Framework?

Answer

 

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

blog comments powered by Disqus