Top 10 ASP.NET MVC Interview Questions and Answers

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

Model-View-Controller (MVC) is an architectural software pattern that basically decouples various components of a web application. By using the 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 the user interface to render the domain data.
  • "Controller" translates user actions into appropriate operations performed on the model.

2. What is ASP.NET MVC?

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

3. What are the differences between ASP.NET MVC and ASP.NET WebForms?

ASP.NET Web Forms use the Page Controller Pattern for rendering layout, whereas ASP.NET MVC uses the Front Controller approach. In the case of the Page Controller Pattern, every page has its own Controller, in other words a 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?

The core features of the 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 the ASP.NET MVC framework?

The request flow for the ASP.NET MVC Framework is as follows.

The request hits the Controller coming from the client. The Controller plays its role and decides which model to use to serve the request, further passing that model to the View that then transforms the model and generates an appropriate response that is rendered to the client.

6. What is Routing in ASP.NET MVC?

In the case of a typical ASP.NET application, incoming requests are mapped to physical files such as an .aspx file. The ASP.NET MVC Framework uses friendly URLs that more easily describe the user's action but are not mapped to physical files.

The 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 the appropriate Controller.

Practically, when a user types a URL in a browser window for an ASP.NET MVC application and presses the "go" button, the routing engine uses routing rules defined in the Global.asax file to parse the URL and determine the path of the corresponding Controller.

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

To pass data from a Controller to a View and in a subsequent request, The ASP.NET MVC Framework provides various options, in other words ViewData, ViewBag and TempData.

Both ViewBag and ViewData are used to to communicate between Controller and corresponding View. But this communication is only for the server call, it becomes null if the 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). Since viewData is a dictionary object it 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 persists for the life of a HTTP Request. So, Tempdata can be used to maintain data between redirects, in other words from one Controller to another Controller.

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

As previously stated about request flows in the ASP.NET MVC Framework, a request coming from a client hits the Controller first. Actually the MVC application determines the corresponding Controller using routing rules defined in Global.asax. And Controllers have specific methods for each user actions. Each request coming to the 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 features of ASP.NET MVC is that it separates the input and UI logic from business logic. The role of the Model in ASP.NET MVC is to contain all application logic including validation, business and data access logic except View. In other words input and Controller; in other words UI logic.

The Model is normally responsible for accessing data from some persistent medium such as a database and manipulates it.

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

If we need to apply some specific logic before or after action methods then 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 way for adding pre-action or post-action behavior to Controller actions.

For example:

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

Here are some useful resources on ASP.NET MVC.
 
 


Similar Articles