Top 10 ASP.NET MVC Interview Questions

By | September 15, 2013

A must have list of ASP.NET MVC Interview Questions and Answers with concepts and necessary code examples. If you understand following key concepts, you will definitely feel more comfortable during an interview. But along with that you need to prepare your practical skills on ASP.NET MVC technology.You can also find more related implementation details here:

To test your ASP.NET MVC knowledge, take a simple MVC Online Test

1. Explain MVC (Model-View-Controller) in general?

MVC (Model-View-Controller) is an architectural software pattern that basically decouples various components of a web application. By using MVC pattern, we can develop applications that are more flexible to changes without affecting the other components of our application.

  •  “Model”, is basically domain data.
  •  “View”, is user interface to render domain data.
  •  “Controller”, translates user actions into appropriate operations performed on model.

2. What is ASP.NET MVC?

ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework.

3. 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.
Follow the link for the difference between the ASP.NET MVC and ASP.NET WebForms.

4. What are the Core features of ASP.NET MVC?

Core features of ASP.NET MVC framework are:

  • Clear separation of application concerns (Presentation and Business Logic)
  • An extensible and pluggable framework
  • Extensive support for ASP.NET Routing
  • Support for existing ASP.NET features

Follow for detailed understanding of above mentioned core features.

5. Can you please explain the request flow in ASP.NET MVC framework?

Request flow for ASP.NET MVC framework is as follows:
Request hits the controller coming from client. Controller plays its role and decides which model to use in order to serve the request. Further passing that model to view which then transforms the model and generate an appropriate response that is rendered to client.

ASP.NET MVC Request Flow

6. 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. ASP.NET MVC framework uses friendly URLs that more easily describe user’s action but not mapped to physical files.
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. You can find ASP.NET MVC Routing further details here.

7. What is the difference between ViewData, ViewBag and TempData?

In order to pass data from controller to view and in next subsequent request, ASP.NET MVC framework provides different options i.e. ViewData, ViewBag and TempData.

Both ViewBag and ViewData are used to to communicate between controller and corresponding view.But this communication is only for server call, it becomes null if redirect occurs. So, in short, its a mechanism to maintain state between controller and corresponding view.

ViewData is a dictionary object while ViewBag is a dynamic property (a new C# 4.0 feature). ViewData being a dictionary object is accessible using strings as keys and also requires typecasting for complex types.On the other hand, ViewBag doesn’t have typecasting and null checks.
TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects i.e from one controller to the other controller.

You can easily find examples for implementation of ViewBag, ViewData and TempData here.

8. What are Action Methods in ASP.NET MVC?

As I already explained about request flow in ASP.NET MVC framework that request coming from client hits controller first. Actually MVC application determines the corresponding controller by using routing rules defined in Global.asax. And controllers have specific methods for each user actions. Each request coming to controller is for a specific Action Method. The following code example, “ShowBooks” is an example of an Action Method.

  public ViewResult ShowBooks(int id)
  {
           var computerBook = db.Books.Where(p => P.BookID == id).First();
           return View(computerBook);
  }

9.Explain the role of Model in ASP.NET MVC?

One of the core feature of ASP.NET MVC is that it separates the input and UI logic from business logic. Role of Model in ASP.NET MVC is to contain all application logic including validation, business and data access logic except view i.e. input and controller i.e UI logic.
Model is normally responsible for accessing data from some persistent medium like database and manipulate it, so you can expect that interviewer can ask questions on database access topics here along with ASP.NET MVC Interview Questions.

10.What are Action Filters in ASP.NET MVC?

If we need to apply some specific logic before or after action methods, we use action filters. We can apply these action filters to a controller or a specific controller action. Action filters are basically custom classes that provide a mean for adding pre-action or post-action behavior to controller actions.
For example,

  • Authorize filter can be used to restrict access to a specific user or a role.
  • OutputCache filter can cache the output of a controller action for a specific duration.

ASP.NET MVC is an amazing framework for developing applications. Above mentioned ASP.NET MVC interview questions must be prepared before appearing for a MVC interview.

To test your ASP.NET MVC knowledge, take a simple MVC Online Test

Top 10 Interview Questions and Answers Series:

  • http://www.blogger.com/profile/06801969378655656273 James Blake

    This comment has been removed by a blog administrator.

  • http://www.blogger.com/profile/16242555948527196019 Vijay Thirugnanam

    The questions are quite simple. I used to work for Microsoft. We ask more questions on extending MVC framework. How will you create a Custom Controller Factory? What are the limitations of the Default Model Binder? How will you use MVC with IoC containers? etc.

  • http://www.blogger.com/profile/10867674357364468276 Imran Ghani

    You are right to some extent. We may go in details for more complex questions but will always start with a key main area and then move forward depending the candidate’s knowledge. That’s why I said, its top 10 questions every ASP.NET MVC developer must know.

  • http://www.blogger.com/profile/03884948907646582471 Uday M

    Dear Imran,
    I have started learning MVC recently

    Please can you explain in MVC 4 how to show the data related to multiple entities in a single view.
    should it be by using ViewModel.
    or give a url link which has the source code or easy to understand.

    For ex: Country,City,State are the 3 entities in Database, and i need to show in view in below format
    Country Name, State Name, District ..

    DataBase First approach should be OK.
    Please do the needfull.
    My mail id is udaym2013@yahoo.co.in
    Regards
    Udaya.M

  • http://www.blogger.com/profile/10867674357364468276 Imran Ghani

    Dear Udaya,
    You will find such examples on MVC in my future articles here in this blog but focus will be MVC 5. You need to follow by Email so that you get the updated results. For specifically on MVC 4, you can google. I’ll try to provide if I find some relevant material.

    Thanks