MVC Interview Questions.
Just a few MVC-related interview questions I've run across.
- What is MVC?
MVC is an acronym for the Model, View Controller framework:
Model - The business object layer of the application
View - the front-end, or customer facing layer of the application
Controller - the business logic layer of the application
- What are the prime advantage of using MVC?
Since the view is separate from the model and controller, it's easier to create unique view that utilize the same Models and Controllers. It is also easier to update the View without changing the Model or Controller.
Separation of Concerns - As MVC denotes separate disciplines, it is easier to delegate coding responsibilities to different teams.
Testability - MVC is better suited to TDD than ASP.NET web form development
- What is the MVC application Life cycle?
CREATING THE REQUEST OBJECT
1. Fill Route - MVC requests are mapped to Route Tables that specify which controller and action to invoke.
2. Fetch Route - Depending on the URL sent, the "UlrRoutingModule" searches the route table to create the "RouteDate" object, which contains the details of which controller and action to invoke.
3. Request Context Created - The "RouteData" object is used to create the "RequestContext" object
4. Controller instance Created - The request object is sent to "MvcHandler" instance to create the Controler class instance. Once the Controller class object is created, the 'Execute" method of the Controller is called.
CREATE THE RESPONSE OBJECT
5. Execute the method defined in step 4.
6. Build the View for the requesting platform.
7. Send the resulting View data to the client.
- What are the different return types of a Controller action method?
ViewResult (view) - Returns a web page
PartialViewResult(PartialView) - Returns a partial view that is to be displayed in a different view.
RedirectToRouteResult(RedirectToAction, RedirectToRoute) - Used to redirect to another action method
ContentResult(Content) - Returns an HTTP content type (ie: text/plain) as a result
JsonResult(json) - Returns a json result set
FileResult(File) - Returns a binary file data response
EmptyResult - Returns noting (void)
- What are Filters in MVC?
FIlters are qualifiers that can be used in MVC to set pre/post behaviors on a Controller's action methods:
Action Filters - Used to implement logic that's executed before and after a Controller action executes.
Authorization Filters - Used to implement authentication and authorization for controller actions.
Result Filters - These contain logic that is executed before and after a View result is executed.
Exception Filters - Used to handle any errors raised by either Controller actions or Controller action results.
- What are the Action Filters in MVC?
Output Cache - caches the output of a controller action for a set amount of time
Handler Error - handles errors raised when a Controller Action executes.
Authorize - enables the developer to restrict access to a given user or role
- What is Routing in MVC?
Routing is the mechanism that processes the incoming URL for an MVC web site. It is generally thought of as more descriptive and able to provide a more detailed response.
- What is the difference between TempData, ViewData and ViewBag?
TempData - Used to pass data from the current request to the next request. Generally used to store 'one-time' messages such as error and validation messages. Requires typecasting for complex types.
ViewData - Used to pass data from the Controller to the View. Requires typecasting for complex types.
ViewBag - Used to pass data from the Controller to the View. This is a dynamic property, so it does not require typecasting for complex types.