Interview questions and Answers for MVC - Model View Cotroller, MVC ASP.NET Interview question and answers
Interview question and answers for ASP.Net MVC, Model View Cotroller Concept, Features
of MVC, Adveneteges of MVC, Page Life Cycle of MVC, View Engine, Routing, ViewBag,
ViewData, TempData, Action, JSON, AJAX
1. What is MVC, Model View Controller Concept?
MVC means Model view controller, It is commonly used for developing software that
divides an application into three interconnected parts.
- Model– It represents the application data domain. In other words applications business
logic is contained within the model and is responsible for maintaining data
- View– It represents the user interface, with which the end users communicates. In
short all the user interface logic is contained within the VIEW
- Controller– It is the controller that answers to user actions. Based on the user
actions, the respective controller responds within the model and choose a view to
render that display the user interface. The user input logic is contained with-in
the controller
2. Waht are the advantages of MVC?
- Faster development process: MVC supports rapid and parallel development. With MVC,
one programmer can work on the view while other can work on the controller to create
business logic of the web application. The application developed using MVC can be
three times faster than application developed using other development patterns.
- Ability to provide multiple views: In the MVC Model, you can create multiple views
for a model. Code duplication is very limited in MVC because it separates data and
business logic from the display.
- Support for asynchronous technique: MVC also supports asynchronous technique, which
helps developers to develop an application that loads very fast.
- Modification does not affect the entire model: Modification does not affect the
entire model because model part does not depend on the views part. Therefore, any
changes in the Model will not affect the entire architecture.
- MVC model returns the data without formatting: MVC pattern returns data without
applying any formatting so the same components can be used and called for use with
any interface.
3. Explain MVC application life cycle?
ASP.Net MVC has two life cycles, The application life cycle and The request life
cycle.
- An instance of the RouteTable class is created on application start. This happens
only once when the application is requested for the first time.
- The UrlRoutingModule intercepts each request, finds a matching RouteData from a
RouteTable and instantiates a MVCHandler (an HttpHandler).
- The MVCHandler creates a DefaultControllerFactory (you can create your own controller
factory also). It processes the RequestContext and gets a specific controller (from
the controllers you have written). Creates a ControllerContext. es the controller
a ControllerContext and executes the controller.
- Gets the ActionMethod from the RouteData based on the URL. The Controller Class
then builds a list of parameters (to to the ActionMethod) from the request.
- The ActionMethod returns an instance of a class inherited from the ActionResult
class and the View Engine renders a view as a web page.
4. What are Action Filters in MVC?
Action Filters are additional attributes that can be applied to either a controller
section or the entire controller to modify the way in which action is executed.
These attributes are special .NET classes derived from System.Attribute which can
be attached to classes, methods, properties and fields.
- Output Cache: This action filter caches the output of a controller action for a
specified amount of time.
- Handle Error: This action filter handles errors raised when a controller action
executes.
- Authorize: This action filter enables you to restrict access to a particular user
or role.
5. What is Route in MVC?
A route is a URL pattern that is mapped to a handler. The handler can be a physical
file, such as a .aspx file in a Web Forms application. A handler can also be a class
that processes the request, such as a controller in an MVC application. To define
a route, you create an instance of the Route class by specifying the URL pattern,
the handler, and optionally a name for the route.
6. What is routing in MVC?
Routing is a mechanism to process the incoming url that is more descriptive and
give desired response. In this case, URL is not mapped to specific files or folder
as was the case of earlier days web sites. Routing is the URL pattern that is mappped
together to a handler,rounting is responsible for incoming browser request for particular
MVC controller. In other ways let us say routing help you to define a URL structure
and map the URL with controller. There are three segments for routing that are important,
- Convention based routing: to define this type of routing, we call MapRoute method
and set its unique name, url pattern and specify some default values.
- Attribute based routing: to define this type of routing, we specify the Route attribute
in the action method of the controller.
7. What is ViewData?
ViewData is used to pass data from controller to view. ViewData is derived from
ViewDataDictionary class. ViewData is available for the current request only. ViewData
requires typecasting for complex data type and checks for null values to avoid error.
If redirection occurs, then its value becomes null.
8. What is ViewBag?
ViewBag is used to pass data from the controller to the respective view. ViewBag
is a dynamic property that takes advantage of the new dynamic features in ASP.Net.
ViewBag is also available for the current request only. If redirection occurs, then
its value becomes null. Doesn’t require typecasting for complex data type.
9. What is TempData?
TempData is derived from TempDataDictionary class. TempData is used to pass data
from the current request to the next request. TempData keeps the information for
the time of an HTTP Request. This means only from one page to another. TempData
helps to maintain the data when we move from one controller to another controller
or from one action to another action TempData requires typecasting for complex data
type and checks for null values to avoid error. TempData is used to store only one
time messages like the error messages and validation messages
10. Explain what is Partial View in MVC?
Partial view is a reusable view (like a user control) which can be embedded inside
other view. A partial view is a chunk of HTML that can be safely inserted into an
existing DOM. Most commonly, partial views are used to componentize Razor views
and make them easier to build and update. Partial views can also be returned directly
from controller methods.
11. What is Razor in MVC?
The Razor View Engine is an advanced view engine that was introduced with MVC 3.0.
This is not a new language but it is markup. Razor is a markup syntax that lets
you embed server-based code into web pages. Server-based code can create dynamic
web content on the fly, while a web page is written to the browser. When a web page
is called, the server executes the server-based code inside the page before it returns
the page to the browser. By running on the server, the code can perform complex
tasks, like accessing databases. Razor is based on ASP.NET, and designed for creating
web applications. It has the power of traditional ASP.NET markup, but it is easier
to use, and easier to learn.
12. What are the Main Razor Syntax Rules?
- Razor code blocks are enclosed in @{ ... }
- Inline expressions (variables and functions) start with @
- Code statements end with semicolon
- Variables are declared with the var keyword
- Strings are enclosed with quotation marks
- C# code is case sensitive
- C# files have the extension .cshtml
13. What are HTML helpers in MVC?
HTML helpers help you to render HTML controls in the view. For instance if you want
to display a HTML textbox on the view ,
14. Where is the route mapping code written?
The route mapping code is written in "RouteConfig.cs" file and registered using
"global.asax" application start event.
15. Can we map multiple URL’s to the same action?
Yes, we can, just need to make two entries with different key names and specify
the same controller and action.
16. What are partial views in MVC?
Partial view is a reusable view (like a user control) which can be embedded inside
other view
17. How can we do validations in MVC?
By using data annotations we can do validation in MVC
18. Can we display all errors in one go?
Yes, we can use the ValidationSummary method from the Html helper class.
19. What is ActionResult?
ActionResult has several derived classes like ViewResult, JsonResult, FileStreamResult,
and so on. ActionResult can be used to exploit polymorphism and dynamism. So if
you are returning different types of views dynamically. An ActionResult is a return
type of a controller method, also called an action method, and serves as the base
class for *Result classes. Action methods return models to views, file streams,
redirect to other controllers, or whatever is necessary for the task at hand
20. What is ViewResult?
ViewResult is a subclass of ActionResult. The View method returns a ViewResult.