1. What is MVC?
Answer:
MVC stands for Model, View, and Controller. MVC is a software architectural pattern which separates the representation and user interaction.
Model represents the real world object and provides data to the view. It is nothing but a set of classes that describes business logic.
View is responsible for look and feel. It represents UI components like HTML, CSS, jQuery etc.
Controller is responsible for taking the end user request via View and loading the appropriate Model and View.
2. How Model, View, Controller communicate each other in ASP.Net MVC?
Answer:
User interacts with the controller.
There is one-to-many relationship between Controller and View which means one controller can mapped to multiple views.
Controller and View can have a reference to model.
Controller and View can talk to each other.
Model and View cannot talk to each other directly. They communicate to each other with the help of controller.
3. What are the advantages of ASP.Net MVC over ASP.Net web form?
Answer:
There are following advantages of using ASP.NET MVC over ASP.NET web form.
i. The MVC framework provides a clean separation of the UI, Business Logic, Data or model.
ii. Automated UI testing is possible because now the behind code (UI interaction code) has moved to a simple .NET class. This gives us opportunity to write unit tests and automate manual testing.
iii. The ASP.NET MVC provides the better control over HTML, CSS and JavaScript than the traditional web form.
iv. The ASP.NET MVC is lightweight because it doesn’t use viewstate and thus reduces the bandwidth of the request.
v. The ASP.NET MVC is supported most of the feature of ASP.NET web form like authentication and authorization, roles, caching, session etc.
4. Explain the difference between 3-layer architecture and MVC architecture.
Answer:
MVC is an evolution of a three layered traditional architecture. Many components of the three layered architecture are the part of MVC.
A fundamental rule in three tier architecture is the client tier never communicate directly with the data tier. In a three-tier model all communication must pass through the middle tier.
MVC architecture is triangular. The view sends updates to the controller, the controller updates the model, and the view gets updated directly from the model.
5. Explain the MVC application life cycle.
Answer:
Any web application first understands the request and depending on type of request, it sends out appropriate response. MVC application life cycle is not different, it has two main phases, first creating the request object and second sending our response to the browser.
The request object creation has four major steps,
Step 1: Fill route
Every MVC request is mapped to route table which specifies which controller and action to be invoked. If it is the first request, it fills the route table with route collection. This filling of route table happens in the
global.asax file.
Step 2: Fetch route
Depending on the URL sent "UrlRoutingModule" searches the route table to create "RouteData" object. RouteData has the details of which controller and action to invoke.
Step 3: Request context created
The "RouteData" object is used to create the "RequestContext" object.
Step 4: Controller instance created
The coming request object is sent to "MvcHandler" instance to create the object of controller class. Once the controller class object is created, it calls the "Execute" method of the controller class.
Creating Response object:
This phase has two steps - Executing the action and finally sending the response as a result to the view.
6. What are the different return types of controller action methods?
Answer:
There are total nine return types to return results from controller to view. The base type of all these result types is ActionResult.
1. ViewResult (View): It is used to return a webpage from an action method.
2. PartialviewResult (Partialview): It is used to send a part of a view which will be rendered in another view.
3. RedirectResult (Redirect): This return type is used to Redirects to another action method by using its URL.
4. RedirectToRouteResult (RedirectToAction, RedirectToRoute): It is used to Redirects to another action method.
5. ContentResult (Content): It returns a user defined content type.
6. JsonResult (Json): This return type is used to return a serialized JSON object.
7. JavaScriptResult(JavaScript): This return type is used to return JavaScript code that will run in browser.
8. FileResult(File): This return type is used to returns binary output to write to the response.
9. EmptyResult(): This return type is used to represent a return value that is used if the action method must return a null result (void).