What is MVC?

MVC is an architectural pattern which separates the representation and the user interaction. It’s divided in to three broader sections, “Model”, “View” and “Controller”. Below is how each one of them handles the task.

The “View” is responsible for look and feel.

The “Model” represents the real world object and provides data to the “View”.

The “Controller” is responsible to take the end user request and load the appropriate “Model” and “View”.

MVC Interview1x1

Explain MVC application life cycle?

Note: – There is nothing as such called as MVC life cycle. I think lot of people are obsessed with ASP.NET page life cycle and they think there is life cycle in MVC as well. To be specific the MVC request goes through various steps of execution and that’s what is termed as MVC application life cycle.

MVC Interview2x1

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. Below is the detail 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 thecontroller 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 theresponse as a result to the view.

Step 5 Execute Action: – The “ControllerActionInvoker” determines which action to executed andexecutes the action.

Step 6 Result sent : – The action method executes and creates the type of result which can be a view result, file result , JSON result etc.

So in all there are six broad steps which get executed in MVC application life cycle.

Note :- In case you are not able to remember the above steps during interview remember the acronym FFRCER (Fight For Respect Can Evoke Revolution)

Is MVC suitable for both windows and web application?

MVC architecture is suited for web application than windows. For window application MVP i.e. “Model view presenter” is more applicable.IfyouareusingWPFandSLMVVMismoresuitableduetobindings.

What are the benefits of using MVC?

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 binding code 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.

Is MVC different from a 3 layered architecture?

MVC is an evolution of a 3 layered traditional architecture. Many components of 3 layered architecture are part of MVC. So below is how the mapping goes.

Functionality 3 layered / tiered architecture Model view controller architecture
Look and Feel User interface.
UI logic User interface. Controller
Business logic /validations Middle layer
Request is first sent to User interface
Accessing data Data access layer. Data access layer.


MVC Interview4x1


What is the latest version of MVC?

When this note was written, four versions where released of MVC. MVC 1 , MVC 2, MVC 3 and MVC 4. So the latest is MVC 5.

What is the difference between each version of MVC?

Below is a detail table of differences. But during interview it’s difficult to talk about all of them due to time limitation. So I have highlighted important differences which you can run through before the interviewer.

Client-Side Validation Razor ASP.NET Web API
Templated Helpers Readymade project Refreshed and modernized default
Areas templates project templates
Asynchronous Controllers HTML 5 enabled templates New mobile project template
Html.ValidationSummary Helper Method Support for Multiple View Many new features to support
DefaultValueAttribute in Action-Method Engines mobile apps
Parameters JavaScript and Ajax Enhanced support for
Binding Binary Data with Model Binders Model Validation asynchronous methods
DataAnnotations AttributesModel-Validator ProvidersNew RequireHttpsAttribute Action FilterTemplated HelpersDisplay Model-Level Errors Improvements

What are HTML helpers in MVC?

HTML helpers help you to render HTML controls in the view. For instance if you want to display a HTML textbox on the view, below is the HTML helper code.

For checkbox below is the HTML helper code. In this way we have HTML helper methods for every HTML control that exists.

What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?

Both of them provide the same HTML output, “HTML.TextBoxFor” is strongly typed while “HTML.TextBox” isn’t.

Below is a simple HTML code which just creates a simple textbox with “CustomerCode” as name.

Below is “Html.TextBoxFor” code which creates HTML textbox using the property name ‘CustomerCode” from object “m”.

In the same way we have for other HTML controls like for checkbox we have “Html.CheckBox” and “Html.CheckBoxFor”.


Explain the importance of MVC model binders?

Model binder maps HTML form elements to the model. It acts like a bridge between HTML UI and MVC model.

MVC Interview6x1

Take the below simple HTML form example:-

Now this form needs to fill the below “Customer” class model. If you see the HTML control name they are different from the class property name. For example HTML textbox control name is “CCode” and the class property name is “CustomerCode”. This mapping code is written in HTML binder classes.

To create a model binder we need to implement “IModelBinder” interface and mapping code needs to be written in the “BindModel” method as shown in the below code.

Now in the action result method we need to use the “ModelBinder” attribute which will attach the binder with the class model.

What are routing in MVC?

Routing helps you to define user friendly URL structure and map those URL structure to the controller.

For instance let’s say we want that when any user types “http://localhost/View/ViewCustomer/” , it goes to the “Customer” Controller and invokes “DisplayCustomer” action. This is defined by adding an entry in to the “routes” collection using the “maproute” function. Below is the under lined code which shows how the URL structure and mapping with controller and action is defined.



Where is the route mapping code written?

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

 What is RouteConfig.cs in MVC 4?

“RouteConfig.cs” holds the routing configuration for MVC. RouteConfig will be initialized on Application_Start event registered in Global.asax.

Can we map multiple URL’s to the same action?

Yes , you can , you just need to make two entries with different key names and specify the same controller and action.

How can we navigate from one view to other view using hyperlink?

By using “ActionLink” method as shown in the below code. The below code will create a simple URL which help to navigate to the “Home” controller and invoke the “GotoHome” action.

How can we restrict MVC actions to be invoked only by GET or POST?

We can decorate the MVC action by “HttpGet” or “HttpPost” attribute to restrict the type of HTTP calls. For instance you can see in the below code snippet the “DisplayCustomer” action can only be invoked by “HttpGet”. If we try to make Http post on “DisplayCustomer” it will throw an error.

Mention what is the difference between Temp data, View, and View Bag?

  • 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


What is partial view in MVC?

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.

 Explain how you can implement Ajax in MVC?

In Ajax, MVC can be implemented in two ways

  • Ajax libraries
  • Jquery

 Mention what is the difference between “ActionResult” and “ViewResult” ?

“ActionResult” is an abstract class while “ViewResult” is derived from “AbstractResult” class.  “ActionResult” has a number of derived classes like “JsonResult”, “FileStreamResult” and “ViewResult” .

“ActionResult” is best if you are deriving different types of view dynamically.

Explain how you can send the result back in JSON format in MVC?

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

Explain what is the difference between View and Partial View?

                            View                                Partial 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
  •  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

List out the types of result in MVC?

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

 Mention what is the importance of NonActionAttribute?

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.

What is Routing in ASP.NET MVC?

In case of a typical ASP.NET application, incoming requests are mapped to physical files such as .aspx file. On the other hand, ASP.NET MVC framework uses friendly URLs that more easily describe user’s action but not mapped to physical files. Let’s see below URLs for both ASP.NET and ASP.NET MVC.

 Hide   Copy Code

 Hide   Copy Code

 Hide   Copy Code

 ASP.NET MVC framework uses a routing engine, that maps URLs to controller classes. We can define routing rules for the engine, so that it can map incoming request URLs to appropriate controller.

Practically, when a user types a URL in a browser window for an ASP.NET MVC application and presses “go” button, routing engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller.

 Explain the methods used to render the views in MVC?

Below are the methods used to render the views from action –

  • View() – To return the view from action.
  • PartialView() – To return the partial view from action.
  • RedirectToAction() – To Redirect to different action which can be in same controller or in different controller.
  • Redirect() – Similar to “Response.Redirect()” in webforms, used to redirect to specified URL.
  • RedirectToRoute() – Redirect to action from the specified URL but URL in the route table has been matched.

Leave a Reply