ASP.NET MVC Interview Questions with Answers

ASP.NET MVC Interview Questions:

1) What are the 3 main components of an ASP.NET MVC application?

1. M - Model
2. V - View
3. C - Controller

2) In which assembly is the MVC framework defined?

System.Web.Mvc

3) What does Model, View and Controller represent in an MVC application?

Model: Model represents the application data domain. In short the applications business logic is contained with in the model.

View: Views represent the user interface, with which the end users interact. In short the all the user interface logic is contained with in the UI.

Controller: Controller is the component that responds to user actions. Based on the user actions, the respective controller, work with the model, and selects a view to render that displays the user interface. The user input logic is contained with in the controller.

4) What are the advantages of ASP.NET MVC?

1. Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested.
2. Complex applications can be easily managed
3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller.
4. ASP.NET MVC views are light weight, as they donot use viewstate.

5) Difference between ASP.NET MVC and ASP.NET WebForms?

ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front controller approach. In case of Page controller approach, every page has its own controller i.e. code-behind file that processes the request. On the other hand, in ASP.NET MVC, a common controller for all pages processes the requests.

6) 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.

<%= Html.TextBox(“Name”)%>

7) 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. 

8) How do you specify comments using razor syntax?

Razor syntax makes use of @* to indicate the begining of a comment and *@ to indicate the end. An example is shown below.
@* This is a Comment *@

9) What is routing in MVC?

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.

10) How can we maintain sessions in MVC?

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

11) What is the difference between tempdata, viewdata, and viewbag?

Temp data - Helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.

View data - Helps to maintain data when you move from controller to view.

View Bag - It’s a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.

12) What are partial views in MVC?

Partial view is a reusable view (like a user control) which can be embedded inside other view. For example let’s say all your pages of your site have a standard structure with left menu, header, and footer. For every page you would like to reuse the left menu, header, and footer controls. So you can go and create partial views for each of these items and then you call that partial view in the main view.

13) How can we do validations in MVC?

One of the easiest ways of doing validation in MVC is by using data annotations. Data annotations are nothing but attributes which can be applied on model properties.

14) Can we display all errors in one go?

Yes, we can; use the ValidationSummary method from the Html helper class.

15) What is Razor in MVC?

It’s a light weight view engine. Till MVC we had only one view type, i.e., ASPX. Razor was introduced in MVC 3.

16) So which is a better fit, Razor or ASPX?

As per Microsoft, Razor is more preferred because it’s light weight and has simple syntaxes.

17) How can you do authentication and authorization in MVC?

You can use Windows or Forms authentication for MVC.

18) What is the difference between ActionResult and ViewResult?

ActionResult is an abstract class while ViewResult derives from the ActionResult class.ActionResult has several derived classes like ViewResult, JsonResult, FileStreamResult, and so on.

19) What are the different types of results in MVC?

There 12 kinds of results in MVC, at the top is the ActionResult class which is a base class that can have 11 subtypes as listed below:

  • ViewResult - Renders a specified view to the response stream
  • PartialViewResult - Renders a specified partial view to the response stream
  • EmptyResult - An empty response is returned
  • RedirectResult - Performs an HTTP redirection to a specified URL
  • RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data
  • JsonResult - Serializes a given ViewData object to JSON format
  • JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
  • ContentResult - Writes content to the response stream without requiring a view
  • FileContentResult - Returns a file to the client
  • FileStreamResult - Returns a file to the client, which is provided by a Stream
  • FilePathResult - Returns a file to the client

20) What are ActionFilters in MVC?

ActionFilters help you to perform logic while an MVC action is executing or after an MVC action has executed.

21) What are the different types of action filters?

Authorization filters
Action filters
Result filters
Exception filters

22) What is bundling and minification in MVC?

Bundling and minification helps us improve request load times of a page thus increasing performance.

23) What is Area in ASP.Net MVC?

Area is used to store the details of the modules of our project. This is really helpful for big applications, where controllers, views and models are all in main controller, view and model folders and it is very difficult to manage.

24) Explain the concept of MVC Scaffolding?

Scaffolding is a technique in which the MVC template helps to auto-generate CRUD code. CRUD stands for create, read, update and delete.

25) How can we do exception handling in MVC?

In the controller you can override the “OnException” event and set the “Result” to the view name which you want to invoke when error occurs. In the below code you can see we have set the “Result” to a view named as “Error”.

26) Explain Keep method in Tempdata in ASP.Net MVC?

As explained above in case data in Tempdata has been read in current request only then "Keep" method has been used to make it available for the subsequent request.

27) Explain what is the difference between View and Partial View?

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 
ASP.NET MVC Interview Questions with Answers ASP.NET MVC Interview Questions with Answers Reviewed by amara sangati for Rating: 5