These ASP.NET MVC questions have been designed for various interviews, competitive exams and entrance tests. We have covered questions on both basic and advanced concepts which will help you improve your skills to face interview questions on ASP.NET MVC.
Who is this ASP.NET MVC interview questions designed for?
All the Web developers, Front End developers, UI/ UX developers and designers will find these questions extremely useful. All freshers, BCA, BE, BTech, MCA and college students wanting to make a career in front end designing will be highly benefitted by these questions.
ASP.NET MVC interview questions topics
This section covers ASP.NET MVC topics like - advantages of ASP.Net MVC, Routing in ASP.NET MVC, Action Methods, ViewData, ViewBag and TempData etc.
1. What is MVC?
MVC stands for Model, View, and Controller. MVC is a software design pattern which separates the business logic, presentation logic and data. It is a concept that makes creating software in any programming language easy. It is widely used for designing web applications and mobile apps.
Model maintains application data and business logic.
View is responsible for look and feel. It displays data to the user.
Controller is responsible for handling user's requests and rendering the appropriate view with the model data.
2. What is ASP.NET MVC? What are the advantages of ASP.Net MVC over ASP.Net web form?
ASP.NET MVC is an open source web development framework which is integrated with the existing ASP.NET features. It is based on MVC architectural design pattern that manages application complexity by separating an application into the model, view and controller.
Advantages of using ASP.NET MVC over ASP.NET web form:
Asp.Net MVC uses MVC pattern based development model that allows separation of program logic from the user interface and makes complex applications easy to manage.
The ASP.NET MVC is lightweight because it doesn’t use viewstate and thus reduces the bandwidth of the request.
ASP.NET MVC is built on top of ASP.NET framework, so it offers full features of ASP.NET.
ASP.NET MVC provides better support for test-driven development.
Asp.Net MVC is suitable for large scale developer team as it relies on loosely coupled layers, so the team can work simultaneously on Model, Views and Controller.
3. What is Routing in ASP.NET MVC?
Routing is a pattern matching system that is responsible for mapping incoming browser requests to particular MVC controller actions. At runtime, the routing engine finds a match in the route table for the incoming request's URL and forwards the request to the appropriate controller and action.
4. What do you mean by Action Methods?
Controllers define action methods for URL mapping. Controller and its action method handle incoming browser requests, retrieve necessary model data and generate appropriate responses in the form of Action Result.
All the public methods in the Controller class are called Action methods.
5. What is the difference between ViewData, ViewBag and TempData?
ViewData, ViewBag, and TempData are used to pass data between controller, action, and views.
ViewData is a dictionary object to pass data from controller to view. We need to typecast the objects in ViewData
ViewBag is a dynamic object to pass data from controller to view. It is a wrapper around ViewData, which allows to create dynamic properties. We don't require to typecast the objects in ViewBag
TempData is a dictionary object to pass data from one controller to another controller. It internally uses session to store data.
6. What is Partial View in ASP.NET MVC?
Partial view in MVC is used to render a portion of view content. It is just like a user control in traditional ASP.NET web forms. It is helpful in reducing code duplication. It allows rendering a view within the parent view.
7. Explain the difference between View and Partial View.
View contains the layout page. Partial view does not contain layout page.
View may have markup tags like html, body, head, title, meta etc. The Partial view is specially designed to render within the view and as a result it does not contain any markup.
View is not lightweight as compare to Partial View.
8. What is ViewStart file? What is the use of ViewStart in ASP NET MVC?
The ViewStart file is much like a normal view and it also has the same extension (.cshtml) but the code in this file gets executed before the code in an individual view is executed. This allows us to specify common functionalities in ViewStart file like defining a default Layout for all pages, sharing information across all views etc. which will be automatically applied to all the Views
We can set the Layout Property in this file. This eliminates the need to include Layout property on each and every view and ensures a common layout page for multiple views.
ViewStart reduces code redundancy and improves maintainability.
9. What are the Filters in MVC? What are the types of Filters?
ASP.NET MVC filters allow adding pre-action and post-action behavior to controller action method. They are executed either before or after an action method is invoked.
ASP.NET MVC Filters are used to perform common functionalities in your application such as Caching, Logging, Error Handling, Authentication and Authorization, etc.
The ASP.NET MVC framework supports following different types of filters. They are executed in the order listed below:
Authentication Filter: It is responsible for checking a valid or invalid user.
Authorization Filter: It is responsible for checking User Access
Action Filter: It performs some logic that is executed before and after a controller action executes.
Result Filter: It performs some operation before or after the execution of view result.
Exception Filter: It can be used as an exception filter to handle errors raised by either your controller actions or controller action results.
10. What are the ways of handling an Error in MVC?
Exception handling is important for every application, whether it is a web application or a window forms application.
There are multiple ways to handle exception in ASP.NET MVC:
1. Using Try catch block
This is the simple manner to trap errors at code level. You just have to identify the places in your code that can potentially throw an exception and use Try catch block to handle exception.
2. Override OnException method
This method allows trapping errors at the whole controller level. It is called whenever there is an unhandled error in the controller.
3. Using HandleError attribute
ASP.Net MVC HandleError attribute provides built-in exception filter. It can be applied over the action method as well as a controller or at the global level. It displays friendly error page when there is an unhandled exception.
11. What is Bundling and Minification in MVC?
12. What is Output Caching in MVC?
Output Caching enables us to cache the content returned by any controller method so that the same content does not need to be generated each time the same controller method is invoked.
Output Caching improves the performance of the MVC application. It reduces network traffic, reduces server round trips, reduces database round trips etc.