Top 28 ASP.NET MVC 5 Interview Questions and Answers
MVC5 QnA Contents :-
- Most Asked ASP.Net MVC 5 Interview Questions
- What is MVC?
- Explain Model, View and Controller in MVC?
- Which assembly defines MVC framework?
- What is the latest version of MVC in .Net development?
- Explain the difference between TempData, ViewData and ViewBag?
- Explain Partial Views in MVC?
- Explain Validation in MVC?
- What is Razor View in MVC?
- What are the Advantages of MVC?
- What are Filters in MVC?
- Difference between Web Forms vs. MVC?
- Explain Html Helpers in MVC?
- What are Action Filters in MVC?
- What are Action Methods in MVC?\
- What is a ViewModel in MVC?
- What is a View Engine in MVC?
- What are the different types of Action Filters in MVC?
- What is Bundling and Minification in MVC?
- Explain Scaffolding in MVC?
- Explain Exception handling in MVC?
- Explain Areas in MVC?
- Explain Layout View in MVC?
- What is WebAPI?
- What is Restful WebAPI?
- What is Output Caching in MVC?
- Explain attribute based routing in MVC?
- What is ASP.Net Core2.0 MVC?
- Explain Major changes in MVC Core 2.0?
MVC Stands for MODEL-VIEW-CONTROLLER is a Software Architectural Pattern.Which has been implemented across multiple development platforms and languages.It is divided into three components Model, View and Controller
View used to display the User interface and basically a HTML template.Model contains real world objects, data and business logic.Controller handles user interaction and loads the appropriate models and views.
The MVC framework is defined in System.Web.Mvc.
MVC 5 is the latest version of MVC architectural pattern as of 2017 which runs on .Net 4.7 framework
The different ways of managing Session in MVC are TempData, ViewData and ViewBag.
TempData is used to move data from one controller to another or from one action to another also it internally uses ViewData.ViewData is used to transfer data from Controller to View.ViewBag is used to transfer data without casting as it uses dynamic keyword and Internally implements ViewData.
Partial Views is a reusable chunk html code that can be inserted into existing View.They are used to display dynamic content in View with Static layout page.It does not contain a layout page and hence doesn't consist of mark up.
Three things are needed to handle validation in MVC : Data annotations which are nothing but attributes which can used on model Properties. ValidateMessageFor which is an html helper class must be used to display the error ModelState.IsValid must be used in controller to take action according to user Response.
Razor is a lightweight View engine introduced in MVC3. Razor Views can be created with two extensions: .cshtml for C# .vbhtml for Visual Basic.
Applications developed are lightweight also it suited to develop applications that are complex.
Separation of Concerns allows for compartmentalization of Presentation (UI)Business which allows high productivity and development time with large teams.
MVC applications have extensible URL Support making it easy to use option for the End user.
MVC testing is independent of Web Server, database or any other classes due to it supporting Test Driven Development (TDD) approach.
Concept of View State is eliminated which allows the building of a light weight
Application while giving full controller to the developer.
MVC patterns has been in use for many years in different web development
Platforms as it popular method of development in larger projects hence far more
Efficient than Code Behind development in Web Forms.
Filters are used to add logic code that will execute before or after a controller executes an Action method.
Types of Filters:
MVC uses Front Controller approach which means there is a common controller for all pages instead of every page having its own controller like in Web Forms which uses code-behind file that will process the request.
MVC architecture has a very clean separation of concern between Controllers, Models and Views unlike Web Forms.
MVC application apply a approach which is stateless as per common HTTP convention different from Web Forms which use View State.
Absence of Life Cycle in MVC leads to a simpler request cycle which reduces load times MVC does not follow the RAD (Rapid Application Development) approach which may slow down development for people but it is better for Custom Application as developer has more Control over the code.
MVC is a recommended approach for Larger Application development where multiple teams are working in conjunction.
Html Helpers in MVC work in almost the same way as Server Control as they are used to generate Html elements which might be needed for links, Images and for form Elements.These Html Helpers are light weight as they do not contain View State and event model attached to them.
The action filter is an attribute that we can apply on a controller action or an entire controller if we need to.
Types of Action Filter:
- OutputCache -It is used cache the output of a controller action for a specified period of time.
- HandleError - It is used to handle errors raised when a controller action executes in MVC.
- Authorize - It enables you to restrict access to a particular user or role.
Action Methods in MVC are required to execute requests and generate responses to the respective request which they generate in the form of Action Result.
Action Methods must always be public and they cannot be overloaded.
Action methods can never be Static.
In Simple words we can display data from Multiple Models in a Single View via View Model.
View Models are generally used only to render views.
It is used to maintain separation of concern.
View Engine is used to render our view into html so that it can be properly displayed in our browser
MVC uses Razor View Engine for which we must use the namespace Web.Razor.
Authorization filters, Action filters, Result filters and Exception filters are the different types of Action Filters in MVC.
Scaffolding in MVC is used to auto-generate CRUD code in MVC template. Where CRUD stands for Create, Read, Update and Delete.
Exception handling is necessary to track errors in our application and display proper message on their occurrence
Exception handling in MVC can be done by the following way:
- Simple try and catch
- Override “On Exception” method
- Using “Handle Error” Attribute
- Inheriting from “HandleErrorAttribute”
- Handling HTTP errors
- Global Error handling in MVC
The Concept of Area is to divide a large application in multiple smaller modules so that they are easy to develop and maintain in the future.
Each Area have their own controllers, models, and views.
Each Functionality Unit is in a different model.
In an application navigating from one page to another some parts of the UI remain the same to avoid creating the same UI on multiple pages and to only change the dynamic
code we use layout page.
Layout page is similar to Master page in ASP Web Forms.
API stands for Application Program Interface.
Web API's are widely used to send and receive http request.
WebAPI can be said to a contract provided by one piece of software to another,they are used implementing a system of structured request and response.
Rest stands for Representational State Transfer.
Rest is an Architectural style for designing networked applications.
It relies on Stateless, Client-Server protocol and almost it's always HTTP.
Rest treats server objects as resources that can created or destroyed.
It can be used in virtually any language.
Hence the API that follows this structure is called Restful WebAPI.
Output Caching is used to greatly improve the performance of a MVC application.
It allows us to cache the content returned a controller so that the same content does not need to be regenerated each time the same controller method is invoked upon request.
It reduces network traffic, reduces server round trips, reduces database round trips etc.
But few things should be kept in mind while using this i.e. we must avoid caching content that are unique per user along with using Caching for content that is accessed frequently and avoiding caching of contents that are rarely used.
This is a new Feature Introduced in MVC 5
By using Route Attribute on top of ActionResult we can define a custom route to navigate to that particular view in our browser.
- It is the biggest redesign of the ASP.Net since ASP.Net 1.0
- ASP.Net Core is no longer based on System.Web.dll
- The ASP.Net Core application can run on either .Net Core or the Full .Net Framework.
- ASP.Net Core applications can be hosted on Windows, Linux and MacOS.
- It is Open source and Community supported.
- This Applications can be hosted on IIS or can be hosted on nginx or other non-windows servers on top of Kestrel Web Server.
- A new structure of MVC is applied in MVC Core 2.0
- This includes Elimination of few files and incorporating the code into Startup class
- While still retaining its Model, Controller and View components structure.
Comparing the Project structure of MVC 5 and MVC Core 2.0 we can clearly see the absences of Global.asax and Web.Config
There is no Content, Script and Font folders as all these contain Static files which are now housed under a single folder in MVC Core 2.0 called wwwroot.
appsettings.json and configuration files which can be customized to any format such as XML, Json etc.
In ASP.Net Core 2.0 router configuration does not have a separate file like in MVC 5 it's incorporated in Startup.cs itself.
Dependency Injection is Inbuilt in ASP.Net Core Applications so we can just create a service and use it with dependency injection unlike in MVC 5 in which we have to separate DI containers.
In ASP.Net Core 2.0 MVC we have IActionResult instead of ActionResult as return type in controller.