Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET

Top 10 ASP.NET MVC Interview Questions

Rate me:
Please Sign up or sign in to vote.
4.60/5 (75 votes)
15 Sep 2013CPOL6 min read 630.1K   106   25
This post lists the top 10 ASP.NET MVC interview questions.
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 get more related stuff at the end of this article but let's first move forward and get answers to following questions.

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.

Image 1

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.

Image 2

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). It reduces complexity that makes it ideal for large scale applications where multiple teams are working.
  • It’s an extensible as well as pluggable framework. We can plug components and further customize them easily.
  • It provides extensive support for URL Routing that helps to make friendly URLs (means friendly for human as well as Search Engines).
  • It supports for Test Driven Development (TDD) approach. In ASP.NET WebForms, testing support is dependent on Web Server but ASP.NET MVC makes it independent of Web Server, database or any other classes.
  • Support for existing ASP.NET features like membership and roles, authentication and authorization, provider model and caching etc.

Follow for detailed understanding of the 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 generates an appropriate response that is rendered to the client.

Image 3

You can follow the link, in order to understand the Complete Application Life Cycle in ASP.NET MVC.

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

//ASP.NET approach Pointing to physical files(Student.aspx) <br />
//Displaying all students<br />
http://locahost:XXXX/Student.aspx

//Displaying a student by Id = 5<br />
http://locahost:XXXX/Student.aspx?Id=5

//ASP.NET MVC approach Pointing to Controller i.e. Student<br />
//Displaying all students<br />
http://locahost:XXXX/Student

//Displaying student by Id = 5<br />
http://locahost:XXXX/Student/5/

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 complete details of ASP.NET MVC Routing 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 communicate between controller and corresponding view. But this communication is only for server call, it becomes null if redirect occurs. So, in short, it's 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.

Image 4

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

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

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.

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

Action methods perform certain operation using Model and return result back to View. As in above example, ShowBook is an action method that takes an Id as input, fetch specific book data and returns back to View as ViewResult. In ASP.NET MVC, we have many built-in ActionResults type:

  • ViewResult
  • PartialViewResult
  • RedirectResult
  • RedirectToRouteResult
  • ContentResult
  • JsonResult
  • EmptyResult
  • and many more...

For a complete list of available ActionResults types with Helper methods, please click here.

Important Note: All public methods of a Controller in ASP.NET MVC framework are considered to be Action Methods by default. If we want our controller to have a Non Action Method, we need to explicitly mark it with NonAction attribute as follows:

C#
[NonAction]
public void MyNonActionMethod() { ….. }    

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

One of the core features 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 means for adding pre-action or post-action behavior to controller actions.

Image 5

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.

Related Web Development Tutorials

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun5-Nov-15 19:57
Humayun Kabir Mamun5-Nov-15 19:57 
GeneralRe: My vote of 5 Pin
Imran Abdul Ghani5-Nov-15 20:28
Imran Abdul Ghani5-Nov-15 20:28 
QuestionMy Vote of 5 Pin
aarif moh shaikh4-Sep-15 1:16
professionalaarif moh shaikh4-Sep-15 1:16 
Question[My vote of 1] Indefinite article Pin
MSDEVTECHNIK31-Aug-15 15:26
MSDEVTECHNIK31-Aug-15 15:26 
GeneralMy vote of 4 Pin
Sibeesh KV16-Sep-14 16:50
professionalSibeesh KV16-Sep-14 16:50 
GeneralMy vote of 5 Pin
Gadadhar Singha11-Sep-14 23:54
Gadadhar Singha11-Sep-14 23:54 
GeneralRe: My vote of 5 Pin
Imran Abdul Ghani12-Sep-14 0:04
Imran Abdul Ghani12-Sep-14 0:04 
GeneralMy vote of 2 Pin
Manoj B. Kalla12-Aug-14 0:11
Manoj B. Kalla12-Aug-14 0:11 
SuggestionGood Post Pin
Rakesh Sinha21-Jul-14 23:26
Rakesh Sinha21-Jul-14 23:26 
QuestionHow do you check for AJAX request with C# in MVC.NET? Pin
Member 1019884815-Jun-14 20:49
Member 1019884815-Jun-14 20:49 
GeneralMy vote of 3 Pin
KanupriyaGoel7-May-14 11:25
KanupriyaGoel7-May-14 11:25 
GeneralMy vote 5 Pin
Nitin S16-Apr-14 20:51
professionalNitin S16-Apr-14 20:51 
GeneralRe: My vote 5 Pin
Imran Abdul Ghani16-Apr-14 21:10
Imran Abdul Ghani16-Apr-14 21:10 
GeneralMy vote of 1 Pin
Weber15026-Feb-14 1:54
Weber15026-Feb-14 1:54 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun19-Feb-14 21:54
Humayun Kabir Mamun19-Feb-14 21:54 
GeneralMy vote of 4 Pin
Member 1030210213-Nov-13 5:15
professionalMember 1030210213-Nov-13 5:15 
GeneralMy vote of 1 Pin
Member 103744191-Nov-13 1:42
professionalMember 103744191-Nov-13 1:42 
GeneralMy vote of 4 Pin
ramanakamma21-Oct-13 2:10
ramanakamma21-Oct-13 2:10 
GeneralMy vote for 3 Pin
Zoltán Zörgő15-Sep-13 20:35
Zoltán Zörgő15-Sep-13 20:35 
GeneralRe: My vote for 3 Pin
Florian Rappl15-Sep-13 20:57
professionalFlorian Rappl15-Sep-13 20:57 
GeneralRe: My vote for 3 Pin
Imran Abdul Ghani15-Sep-13 21:00
Imran Abdul Ghani15-Sep-13 21:00 
GeneralRe: My vote for 3 Pin
Zoltán Zörgő15-Sep-13 21:15
Zoltán Zörgő15-Sep-13 21:15 
GeneralRe: My vote for 3 Pin
K Sonu20-Aug-14 0:06
K Sonu20-Aug-14 0:06 
GeneralRe: My vote for 3 Pin
Zoltán Zörgő20-Aug-14 1:48
Zoltán Zörgő20-Aug-14 1:48 
GeneralMy vote of 3 Pin
Akhil Mittal15-Sep-13 19:07
professionalAkhil Mittal15-Sep-13 19:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.